分享

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

 chensirDSP 2010-04-28

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多