VC++中启动,暂停,恢复和结束后台工作进程的方法 - My digital life -...
来源:百度文库 编辑:神马文学网 时间:2024/05/23 22:29:51
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();