一个Event,多个响应的问题
先上代码:
HANDLE g_hEvent=NULL;BOOL g_Run=TRUE;TCHAR g_buf[0x100]={0};unsigned int WINAPI EvenThread(void *pVoid){ OutputDebugString(TEXT("start")); while(g_Run) { WaitForSingleObject(g_hEvent,INFINITE); if(g_Run) { OutputDebugString(g_buf); } } OutputDebugString(TEXT("end")); return 0;}//Start Eventvoid CTestSTLDlg::OnBnClickedButton12(){ if(g_hEvent==NULL) { g_Run=TRUE; g_hEvent=CreateEvent(NULL,FALSE,FALSE,TEXT("myTestEvent"));//自动重置信号 _beginthreadex(NULL,0,EvenThread,NULL,0,NULL); } else { OnBnClickedButton13(); }}//Stop Eventvoid CTestSTLDlg::OnBnClickedButton13(){ g_Run=FALSE; SetEvent(g_hEvent); CloseHandle(g_hEvent); g_hEvent=NULL;}//Notify Eventvoid CTestSTLDlg::OnBnClickedButton14(){ SetEvent(g_hEvent); GetDlgItemText(IDC_EDIT4,g_buf,0x100);}
hSemaphore = CreateSemaphore( NULL, // default security attributes 0, // initial count cThreadCount, // maximum count NULL); // unnamed semaphore
[解决办法]
方法1、人工重置 + PulseEvent,但微软说她这个孩子不是很靠谱。
方法2、试试SignalObjectAndWait,(微软新生的,原子的,我也没用过)
都不好,你这个我问题的确有点难搞
人工重置和自动重置事件都不可以实现
你可以在进程启动的时候,注册消息,通过自动重置事件发送消息,三个进程都可以执行
这个是最好的了