Home > Software engineering >  Multithreaded SetEvent problem
Multithreaded SetEvent problem

Time:01-05

Debugging found when pulled up to the inside of the thread function MyDbgPrintf (" 1 ");
While (: : WaitForSingleObject (pDialog_Tab3 - & gt; M_hEvent_CareAllPets, INFINITE)) this way, ask why!!!!!
 void CDialog_Tab3: : OnBnClickedCheckAutoCarePets () 
{
//TODO: add the control notification handler code
//if the thread handle null to create a thread
Char sz [5]={0};
GetDlgItemText (IDC_EDIT_AUTO_CARE_PETS, sz, sizeof (sz));
If (strlen (sz)==0)
{
MessageBox (" please fill in the time interval ", "", MB_OK);
((CButton *) GetDlgItem (IDC_CHECK_AUTO_CARE_PETS)) - & gt; SetCheck (BST_UNCHECKED);
return;
}

If (m_bCheckCareAllPets==FALSE)
{
M_bCheckCareAllPets=TRUE;
((CEdit *) GetDlgItem (IDC_EDIT_AUTO_CARE_PETS)) - & gt; The EnableWindow (FALSE);
_beginthreadex (NULL, 0, ThreadProc_CareAllPets, this, 0, 0).
M_hEvent_CareAllPets=CreateEvent (0, FALSE, FALSE, NULL);
M_hEvent_IsCareAllPetsStop=CreateEvent (0, FALSE, FALSE, NULL);
SetEvent (m_hEvent_CareAllPets);
The SetTimer (IDT_TIMER_CareAllPets, atoi (sz) * 60000, NULL);

return;
}

//if you uncheck the
M_bCheckCareAllPets=FALSE;
((CEdit *) GetDlgItem (IDC_EDIT_AUTO_CARE_PETS)) - & gt; The EnableWindow (TRUE);
SetEvent (m_hEvent_CareAllPets);
: : WaitForSingleObject (m_hEvent_IsCareAllPetsStop, INFINITE);
MyDbgPrintf (" 2 ");
The CloseHandle (m_hEvent_CareAllPets);
The CloseHandle (m_hEvent_IsCareAllPetsStop);
M_hEvent_CareAllPets=NULL;
M_hEvent_IsCareAllPetsStop=NULL;
KillTimer (IDT_TIMER_CareAllPets);
MyDbgPrintf (" cancel the automatic feed pet ");

Void CDialog_Tab3: : OnTimer (UINT_PTR nIDEvent)
{
The switch (nIDEvent)
{
Case IDT_TIMER_CareAllPets:
{
SetEvent (m_hEvent_CareAllPets);
}
}
}

Unsigned int __stdcall ThreadProc_CareAllPets (void * pArg)
{
PArg CDialog_Tab3 * pDialog_Tab3=(CDialog_Tab3 *);
CPet CMyPet;
MyDbgPrintf (" 1 ");
While (: : WaitForSingleObject (pDialog_Tab3 - & gt; M_hEvent_CareAllPets, INFINITE))
{
MyDbgPrintf (" 3 ");
CMyPet. GetData ();
for (int i=0; i {
MyDbgPrintf (" 4 ");
If (pDialog_Tab3 - & gt; M_bCheckCareAllPets==FALSE);
{
SetEvent (pDialog_Tab3 - & gt; M_hEvent_IsCareAllPetsStop);
return 1;
}
While (CMyPet. GetData () - & gt; GetPetInfo () [I] PhysicalPower & lt; 1000 | | CMyPet. GetData () - & gt; GetPetInfo () [I] Mood & lt; 0 x5a)
{
MyDbgPrintf (" 5 ");
If (pDialog_Tab3 - & gt; M_bCheckCareAllPets==FALSE);
{
SetEvent (pDialog_Tab3 - & gt; M_hEvent_IsCareAllPetsStop);
return 1;
}
CMyPet. FeedPet (I + 1);
Sleep (1000);
}
}
MyDbgPrintf (" 6 ");
ResetEvent (pDialog_Tab3 - & gt; M_hEvent_CareAllPets);
//hint
MyDbgPrintf (" all pet care!" );
}
}

}


CodePudding user response:

Try m_hEvent_CareAllPets=CreateEvent (0,/* */TRUE or FALSE, FALSE, NULL);
BManualReset MSDN the instructions on the
If this parameter is TRUE, the function creates a manual - reset the event object, which requires the use of the ResetEvent function to set the event the state to nonsignaled. If this parameter is FALSE, the function creates an auto - reset the event object, and the system automatically resets the event the state to nonsignaled after a single waiting thread has had been released.
  • Related