VC++中启动,暂停,恢复和结束后台工作进程的方法

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();

你可能感兴趣的:(vc++)