简体   繁体   English

C ++事件信号和后台线程

[英]C++ Event signal and background thread

Here is the issue that I have in c++ code implementation. 这是我在c ++代码实现中遇到的问题。

In the main thread , created a dialogbox showing static text with cancel button which also spins a child thread. 在主线程中,创建一个对话框,该对话框显示带有“取消”按钮的静态文本,该对话框也旋转一个子线程。

In the back ground thread ( or the child thread) check the database to see if the certain status field is updated. 在后台线程(或子线程)中,检查数据库以查看特定状态字段是否已更新。 If updated, then return true, otherwise continue to poll the database at regular intervals. 如果已更新,则返回true,否则继续定期轮询数据库。

Expected behavior - Show dialog box with cancel button , continue showing it until the event is signaled by child thread or the user hits cancel button. 预期行为-显示带有“取消”按钮的对话框,继续显示该对话框,直到子线程发出事件信号或用户单击“取消”按钮为止。

I created an event in the main thread with unsignalled state , this event state will be modified by the child tread ( when the status field is updated ). 我在主线程中以无信号状态创建了一个事件,该事件状态将由子级修改(当status字段更新时)。

The problem is if i block the main thread until it gets response from the background thread, the user cannot hit the cancel button in the dialog , it is always accompanied with hour glass symbol. 问题是,如果我阻塞主线程,直到它从后台线程得到响应,则用户无法单击对话框中的“取消”按钮,它总是带有沙漏符号。

Not sure what is wrong with the below code. 不知道下面的代码有什么问题。

HANDLE mainThread = NULL;
HANDLE ghWriteEvent; 

MainMethod()
{
mainThread = GetCurrentThread() ;
dlgCancelDialog dog // dialog with cancel button .

    ghWriteEvent = CreateEvent( NULL, TRUE,  FALSE,  TEXT("WriteEvent") ); 
HANDLE hThread = CreateThread(0,0, childThread, &threadData,0,NULL);
dlg.showDialog(); // Show dialog with cancel button .

    DWORD  dwWaitResult = WaitForSingleObject( ghWriteEvent, INFINITE); 
 //AT THIS POINT i WANT TO SAY WAIT FOR SIGNAL EVENT OR WAIT UNTIL CANCEL OPERATION IS HIT , BUT THE DIALOG IS SHOWN WITH HOURGLASS SYMBOL.
  switch (dwWaitResult) 
   {
    // Event object was signaled
    case WAIT_OBJECT_0: 
   break;
  }
dlg.hideDialog();

} //Child thread code. } //子线程代码。

childThread(LPVOID lpParam)
{
while(!databaseIsUpdated) // Check database.
{   
    Sleep(1000);

}
if (! SetEvent(ghWriteEvent) ) 
    {

        return 0;
    }
return 0;
}

You should not wait with INFINITE because you block your dialog thread. 您不应该等待INFINITE,因为您阻塞了对话线程。

So my proposal is, create timer using SetTimer(hDlg, 500, 1, NULL) and handle WM_TIMER inside DlgProc like this: 所以我的建议是,使用SetTimer(hDlg,500,1,NULL)创建计时器,并在DlgProc中处理WM_TIMER,如下所示:

case WM_TIMER:
{
 DWORD  dwWaitResult = WaitForSingleObject( ghWriteEvent, 0);  //We will not wait

 if(dwWaitResult  == WAIT_OBJECT_0)  //We are signaled, exit now
    dlg.hideDialog();
}
break;

This will check every 500ms state of event.If signaled, dialog ends, if not, continue. 这将每500ms检查一次事件状态。如果发出信号,则对话框结束,否则,继续。

你也可以使用MsgWaitForMultipleObjectsEx(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms684245%28v=vs.85%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM