Original link Address: http://blog.csdn.net/akof1314/article/details/5761689
Worker threads are often used to perform background tasks such as data calculation, background disinfection, and so on. Because you do not need to create windows and process user messages, writing is easier, and you can create and start a worker thread as soon as you call the AfxBeginThread function in your program.
The prototype of AfxBeginThread is as follows:
cwinthread*AfxBeginThread (Afx_threadproc pfnthreadproc, LPVoid Pparam,intNpriority =thread_priority_normal, UINT nstacksize=0, DWORD dwcreateflags=0, Lpsecurity_attributes lpsecurityattrs=NULL); CWinThread*AfxBeginThread (CRuntimeClass*Pthreadclass,intNpriority =thread_priority_normal, UINT nstacksize=0, DWORD dwcreateflags=0, Lpsecurity_attributes lpsecurityattrs=NULL);
As you can see from the above parameter, only the first two parameters are required, and the remaining parameters are used by default, creating a thread with a normal priority level.
Example: Write an application that starts a thread when the left mouse button is pressed in the program window, and the thread can display an informational box on the screen.
1. Create a new single document program;
2. Add a function to prepare as a thread in the view class CPP file:
UINT messagethread (lpvoid pparam) { LPTSTR = (LPTSTR) pparam; *pmainwnd = AfxGetMainWnd (); :: MessageBox (pMainWnd->m_hwnd, PMessage, _t ("Thread Message" ), MB_OK); return 0 ; }
3. Create and start a thread in the left mouse button message response function of the view class:
void Cthreadtestview::onlbuttondown (UINT nflags, CPoint point) { AfxBeginThread (Messagethread, _t ( "Greetings from your thread! " )); Cview::onlbuttondown (nflags, point); }
Program Run Result:
Use of worker thread AfxBeginThread