Use of worker thread AfxBeginThread

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.