Use of user interface thread AfxBeginThread

Source: Internet
Author: User

Original link Address: http://blog.csdn.net/akof1314/article/details/5762027

The user interface thread runs with a window interface and its corresponding window function, so it can interact with the user by responding to the message.

The AfxBeginThread function is prototyped 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);

The user interface thread must contain a message loop so that the user message can be processed. To use a user interface thread, you must derive from a thread class from the CWinThread class, and generally override the InitInstance () and ExitInstance () functions of the class.

Example: Write an application that launches a user-interface thread when the user presses the left mouse button in the main program window. A message box pops up when the user presses the left mouse button in the Thread window interface.
1. Create a new single document program;
2. Click on the menu bar "project" → "Add Class" → "MFC Class", base class Select "CWinThread", class name Input cmythread, click "Finish", and take similar steps to CFrameWnd class as base class to derive Cmywnd class;
3. In the View class implementation file contains the header file:

" MyThread.h "

4. Include the header file in the Cmythread class implementation file:

" MyWnd.h "

5. Create a form in the thread in Cmythread::initinstance ():

BOOL cmythread::initinstance () {     new  Cmywnd ();     Pframewnd->create (NULL, _t ("Thread Windows"  ));     Pframewnd, ShowWindow (sw_show);     Pframewnd, return  TRUE;} 

6. In the Cmywnd class, modify the constructor Cmywnd () to the public property, and then implement the left mouse button message response function:

void   Cmywnd::onlbuttondown (UINT nflags, CPoint point) {     = _t ("Theisa window thread "  );      *pmainwnd = AfxGetMainWnd ();     :: MessageBox (NULL, PMessage, _t ("Thread Message " , MB_OK));    Cframewnd::onlbuttondown (nflags, point); }

7. Implement the left mouse button response function in the application view class:

void   Cthreadtestview::onlbuttondown (UINT nflags, CPoint point) {     AfxBeginThread (Runtime_class (cmythread ));     Cview::onlbuttondown (nflags, point); }

Program Run Result:

It is important to note that this user interface is not part of the main frame window, but is also tied to the main thread window. By looking at the desktop taskbar, you can see that two windows are displayed in parallel, with no effect on one another. Note that the code given above the MessageBox is a reference API, and is windowless, so there are three parallel windows, and do not interfere. Closes the form of the user interface thread separately, which exits the user interface thread normally. However, if the main thread form is closed directly, the user interface thread shuts down illegally, causing a memory leak.

For the normal exit of the user interface thread, as long as the postquitmessage is called within the user interface thread, the parameter is 0, which represents a successful completion.

Use of user interface 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.