1.创建进程:

::AfxBeginThread(BkThreadProc, this->GetSafeHwnd());

2.后台进程函数和控制线程暂停/恢复/退出的事件变量:

CEvent g_EventThreadPause(FALSE,TRUE);
CEvent g_EventThreadQuit(FALSE,TRUE);

UINT BkThreadProc(LPVOID pParam)
{
 TRACE("Start background thread.\n");
 while (TRUE)
 {

  //Check whether the backgound thread need to quit
  if (::WaitForSingleObject(g_EventThreadQuit,0) == WAIT_OBJECT_0)
  {

   //Clean resource before quiting background thread
   TRACE("Quit background thread.\n");
   break;
  }

  //Check whether the backgound thread need to pause

  ::WaitForSingleObject(g_EventThreadPause, INFINITE);

  //.......

  //Do some background work here......

  //.......

  //Sleep(2000);
 }

 return 0;
}

3.暂停线程

g_EventThreadPause.ResetEvent();

4.恢复线程

g_EventThreadPause.SetEvent();

5.退出线程

g_EventThreadPause.SetEvent();
 g_EventThreadQuit.SetEvent();

相关文章:

  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-12-12
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-03
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案