在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
HANDLE m_EvtThreadExit[MaxVisionNum]; //定义 方法一 for(int i=0;i<MaxVisionNum;i++) m_EvtThreadExit[index] = CreateEvent(NULL,false,false,NULL); //手动创建 //线程函数 UINT CMilTestDlg::VisionThread(LPVOID param) { TThreadParam * pParam = (TThreadParam *)param; CMilTestDlg * pDlg = (CMilTestDlg *)(pParam->pThis); int visionIndex = pParam->visionIndex; pDlg->VisionProcess(visionIndex); SetEvent(m_EvtThreadExit[index]); //线程函数最后,设置事件 return 1; } 方法二 for (int i=0;i<MaxVisionNum;i++) { threadParma[i].pThis = this; threadParma[i].visionIndex = i; CWinThread *pthread = AfxBeginThread( VisionThread, &(threadParma[i]), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED ); pthread->m_bAutoDelete = false; ////关闭句柄自动删除 m_EvtThreadExit[i] = pthread->m_hThread; //关联CWinThread句柄 pthread->ResumeThread(); } 方法三 for (int i=0;i<MaxVisionNum;i++) { threadParma[i].pThis = this; threadParma[i].visionIndex = i; m_EvtThreadExit[i] = CreateThread(NULL,0,ThreadProc,&(threadParma[i]),0,NULL); //关联句柄 } DWORD WINAPI ThreadProc(LPVOID lpParam) { TThreadParam * pParam = (TThreadParam *)lpParam; CMilTestDlg * pDlg = (CMilTestDlg *)(pParam->pThis); int visionIndex = pParam->visionIndex; pDlg->VisionProcess(visionIndex); return 1; } /*****************************通用 **************************************/ //判断退出, void CMilTestDlg::DoEvents() { MSG msg; while (::PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } void CMilTestDlg::WaitAllThreadExit(DWORD time) { DWORD t=::GetTickCount(); while(::GetTickCount() -t < time) { DoEvents(); //这个很关键,否则线程与UI相关时,会卡在里面
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论