Void CtestDlg: : OnBnClickedButton1 ()
{
NWaitEvent=CreateEvent (NULL, TRUE, TRUE, _T (" tickets "));
AfxBeginThread (ThreadDataServe, this);
}
UINT CtestDlg: : ThreadDataServe (LPVOID lpParam)
{
LpParam CtestDlg * pDlg=(CtestDlg *);
While (true)
{
for(int i=0; i<10000; I++)
{
PDlg - & gt; SetDlgItemInt (IDC_COUNT, I);
}
: : ResetEvent (nWaitEvent);
: : Sleep (2000);
}
return 1;
}
Void CtestDlg: : OnBnClickedButton2 ()
{
The WaitForSingleObject (nWaitEvent, INFINITE);//
: : SuspendThread (threadHandle);
AfxMessageBox (_T (" OK "));
: : ResumeThread (threadHandle);
}
Want to achieve in the waiting thread in OnBnClickedButton2 function complete end of for loop, but the WaitForSingleObject (nWaitEvent, INFINITE); Always return not, feeling for the use of the event is not quite right, how can validate the OnBnClickedButton2 medium to one end of the for loop to do other things???????
Thank you,,,,
CodePudding user response:
The usage is wrong, CreateEvent you create is a manual reset event, and the initial state is signal, WaitForSingle... () until the EVENT to Signal state,ResetEvent is to set up the EVENT to no signal state,/
Let's have a good look at the MSDN documentation ~
CodePudding user response:
Write wrong, the original code is like this:Void CtestDlg: : OnBnClickedButton1 ()
{
NWaitEvent=CreateEvent (NULL, TRUE, TRUE, _T (" tickets "));
AfxBeginThread (ThreadDataServe, this);
}
UINT CtestDlg: : ThreadDataServe (LPVOID lpParam)
{
LpParam CtestDlg * pDlg=(CtestDlg *);
While (true)
{
: : ResetEvent (nWaitEvent);
for(int i=0; i<10000; I++)
{
PDlg - & gt; SetDlgItemInt (IDC_COUNT, I);
}
SetEvent (nWaitEvent);
: : Sleep (2000);
}
return 1;
}
Void CtestDlg: : OnBnClickedButton2 ()
{
The WaitForSingleObject (nWaitEvent, INFINITE);//
: : SuspendThread (threadHandle);
AfxMessageBox (_T (" OK "));
: : ResumeThread (threadHandle);
}
CodePudding user response: