2008年8月11日 星期一

make a thread in MFC - using AfxBeginThread

MFC有特別提供一個function
AfxBeginThread
負責啟動thread

宣告的方式
AfxBeginThread(FunctionName,NULL,Priority,0,0,NULL);

UINT FunctionName(LPVOID lParam)
{
return NULL;
}
function的實做
在呼叫AfxBeginthread之前

Priority可以設定成以下幾種
  • THREAD_PRIORITY_TIME_CRITICAL
  • THREAD_PRIORITY_HIGHEST
  • THREAD_PRIORITY_ABOVE_NORMAL
  • THREAD_PRIORITY_NORMAL
  • THREAD_PRIORITY_BELOW_NORMAL
  • THREAD_PRIORITY_LOWEST
  • THREAD_PRIORITY_IDLE
那該如何關掉thread
按照msdn的建議 - Multithreading: Terminating Threads
AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.

所以會希望你在實做thread function內去關掉thread

Update 20080828
發現
在離開AfxBeginThread's FunctionName會跑到
\...\Mfc\Src\thrdcore.cpp
會呼叫到
// cleanup and shutdown the thread
threadWnd.Detach();
AfxEndThread(nResult);

看來該是不用在FunctionName額外在去寫AfxEndThread結束thread

透過Facebook分享

沒有留言: