多线程的SetEvent问题

_Wanderer2199 2021-01-04 12:17:13
调试时发现停在了线程函数里面的MyDbgPrintf("1");
while (::WaitForSingleObject(pDialog_Tab3->m_hEvent_CareAllPets, INFINITE))这一句,请教原因!!
void CDialog_Tab3::OnBnClickedCheckAutoCarePets()
{
// TODO: 在此添加控件通知处理程序代码
//如果线程句柄为空 创建一个线程
char sz[5] = { 0 };
GetDlgItemText(IDC_EDIT_AUTO_CARE_PETS, sz, sizeof(sz));
if (strlen(sz) == 0)
{
MessageBox("请填写时间间隔", "", MB_OK);
((CButton*)GetDlgItem(IDC_CHECK_AUTO_CARE_PETS))->SetCheck(BST_UNCHECKED);
return;
}

if (m_bCheckCareAllPets == FALSE)
{
m_bCheckCareAllPets = TRUE;
((CEdit*)GetDlgItem(IDC_EDIT_AUTO_CARE_PETS))->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);
SetTimer(IDT_TIMER_CareAllPets, atoi(sz) * 60000, NULL);

return;
}

//如果取消勾选
m_bCheckCareAllPets = FALSE;
((CEdit*)GetDlgItem(IDC_EDIT_AUTO_CARE_PETS))->EnableWindow(TRUE);
SetEvent(m_hEvent_CareAllPets);
::WaitForSingleObject(m_hEvent_IsCareAllPetsStop,INFINITE);
MyDbgPrintf("2");
CloseHandle(m_hEvent_CareAllPets);
CloseHandle(m_hEvent_IsCareAllPetsStop);
m_hEvent_CareAllPets = NULL;
m_hEvent_IsCareAllPetsStop = NULL;
KillTimer(IDT_TIMER_CareAllPets);
MyDbgPrintf("取消自动喂宠");

void CDialog_Tab3::OnTimer(UINT_PTR nIDEvent)
{
switch (nIDEvent)
{
case IDT_TIMER_CareAllPets:
{
SetEvent(m_hEvent_CareAllPets);
}
}
}

unsigned int __stdcall ThreadProc_CareAllPets(void* pArg)
{
CDialog_Tab3* pDialog_Tab3 = (CDialog_Tab3*)pArg;
CPet CMyPet;
MyDbgPrintf("1");
while (::WaitForSingleObject(pDialog_Tab3->m_hEvent_CareAllPets, INFINITE))
{
MyDbgPrintf("3");
CMyPet.GetData();
for (int i = 0; i < CMyPet.GetPetNum(); i++)
{
MyDbgPrintf("4");
if (pDialog_Tab3->m_bCheckCareAllPets == FALSE);
{
SetEvent(pDialog_Tab3->m_hEvent_IsCareAllPetsStop);
return 1;
}
while (CMyPet.GetData()->GetPetInfo()[i].PhysicalPower < 1000 || CMyPet.GetData()->GetPetInfo()[i].Mood < 0x5A)
{
MyDbgPrintf("5");
if (pDialog_Tab3->m_bCheckCareAllPets == FALSE);
{
SetEvent(pDialog_Tab3->m_hEvent_IsCareAllPetsStop);
return 1;
}
CMyPet.FeedPet(i + 1);
Sleep(1000);
}
}
MyDbgPrintf("6");
ResetEvent(pDialog_Tab3->m_hEvent_CareAllPets);
//提示
MyDbgPrintf("全部宠物照顾完成!");
}
}

}


...全文
246 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
an_bachelor 2021-01-06
  • 打赏
  • 举报
回复
引用 3 楼 _Wanderer2199 的回复:
[quote=引用 1 楼 zgl7903 的回复:]试试 m_hEvent_CareAllPets = CreateEvent(0, /*FALSE*/TRUE, FALSE, NULL); bManualReset MSDN上的说明 If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.
比如我想把【 [ebp - 4] - 44 + 458//大区ID 2字节,[ebp - 4] - 44 + 45A//小区ID 2字节 】这2个数据组成一个DWORD类型,用LOWORD和HIWORD取数据[/quote] 高位的word右移出一个word(16bits)就能放下低位的word ((DWORD)(WORD)hi_word << 16) | (DWORD)(WORD)low_word
zgl7903 2021-01-06
  • 打赏
  • 举报
回复
_Wanderer2199 2021-01-06
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:
试试 m_hEvent_CareAllPets = CreateEvent(0, /*FALSE*/TRUE, FALSE, NULL); bManualReset MSDN上的说明 If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.
比如我想把【 [ebp - 4] - 44 + 458//大区ID 2字节,[ebp - 4] - 44 + 45A//小区ID 2字节 】这2个数据组成一个DWORD类型,用LOWORD和HIWORD取数据
_Wanderer2199 2021-01-06
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903的回复:
试试 m_hEvent_CareAllPets = CreateEvent(0, /*FALSE*/TRUE, FALSE, NULL); bManualReset MSDN上的说明 If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.
请问前辈如何将两个word组成一个dword 使用的时候我希望用宏loword和hiword分别取出数据
zgl7903 2021-01-04
  • 打赏
  • 举报
回复
试试 m_hEvent_CareAllPets = CreateEvent(0, /*FALSE*/TRUE, FALSE, NULL); bManualReset MSDN上的说明 If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the ResetEvent function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released.

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧