Go Two ways to update the contents of a control in an MFC child thread

Source: Internet
Author: User
Tags message queue

I. Overview

Each system is wired (at least one main thread), and the most important role of threads is parallel processing, which increases the concurrency rate of the software. For the interface, it can also improve the response ability of the interface. In general, for application stability, time-consuming operations such as data processing will run separately in one thread, and all control data refreshes related to the main UI thread should be processed in the main UI thread. That is, the data processing thread sends messages, allowing the interface UI to update the control. In the MFC thread is divided into interface threads and worker threads, the interface is actually a thread to draw something, this thread maintains a "message queue", "Message queue" is also the interface thread and worker thread the biggest difference, the word should go into your mind, deep-rooted! There are two types of threads in MFC, called Worker threads and user interface threads, respectively. The main difference between the two is that the worker thread has no message loop, and the user interface thread has its own message queue and message loop.

In MFC, a global function afxbeginthread () is generally used to create and initialize a thread (worker threads, and an overloaded form for creating user-interface Threads). Function Prototypes:

cwinthread* AfxBeginThread (     afx_threadproc pfnthreadproc,     lpvoid pparam,     int npriority = thread_priority_normal,     0,     0,     =  NULL   );  

Return value: Returns a pointer to the thread object of the new thread when successful, otherwise null.

Pfnthreadproc: The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam), cannot be set to NULL. If it is a class member function, it must be a static member function.

Pparam: Arguments passed into the thread, note that it is of type: LPVOID, so we can pass a struct or class object to the thread. This pointer is generally passed to facilitate the invocation of non-static members of the class, because the thread function is a static function.

Npriority: The priority of a thread, typically set to 0, gives it a common priority with the primary thread.

Nstacksize: Specifies the size of the stack for the newly created thread. If 0, the newly created thread has a stack of the same size as the main thread.

Dwcreateflags: Specifies how the line threads after the thread is created. You can specify two values: create_suspended: After a thread is created, it is suspended until it is called: ResumeThread. 0: Start running when the thread is created.

Lpsecurityattrs: points to a security_attributes structure that is used to flag the security of the newly created thread. If NULL, then the newly created thread will have the same security as the main path.

Common usage:

This

The pass-through thread parameter is this, which is the class itself, in order to be able to get the class non-static member variable in the thread function, because the threading function is a static function.

There are two ways to update the contents of a control in an MFC child thread, one is to update the control content through a global function in a child thread, and to update the control content by sending a custom message in a child thread.

Ii. updating the contents of a control with a global function

1. Add member variables in the dialog class Cthreaddemodlg--pointers and thread functions for thread objects

CWinThread *m_pthread;   Static UINT threadfunction (LPVOID pparam);  

2. Implement thread functions, use global functions:: SetWindowText,:: GetDlgItem to update control contents

UINT cthreaddemodlg::threadfunction (lpvoid pparam) {Cthreaddemodlg*pdlg = (CTHREADDEMODLG *) Pparam;  while(TRUE) {:: SetWindowText (:: GetDlgItem (Pdlg->m_hwnd, idc_static), L"Hello World"); Sleep ( +); :: SetWindowText (:: GetDlgItem (Pdlg->m_hwnd, idc_static), L"Hello Android"); Sleep ( +); }      return 0; }  

3. Create a thread in the member function OnInitDialog and start

this);   
Iii. updating the contents of a control by sending a custom message

1. Define the message ID in the header file

#define Wm_update_static (Wm_user + 100)  

2. Adding members to the dialog class Cthreaddemodlg--pointers to thread objects and thread functions

CWinThread *m_pthread;   Static UINT threadfunction (LPVOID pparam);  

3. Declaring a custom message function

afx_msg LRESULT onupdatestatic (WPARAM WPARAM, LPARAM LPARAM);  

4. Add a message map to the CPP file

Begin_message_map (Cthreaddemodlg, CDialog)       // ......      On_message (wm_update_static, &cthreaddemodlg::onupdatestatic)      //...   End_message_map ()  

5. Implementing a custom message response function

LRESULT cthreaddemodlg::onupdatestatic (WPARAM WPARAM, LPARAM LPARAM)  {      if0) {          GetDlgItem (idc_static)->setwindowtext (L"Hello Linux");       Else {          GetDlgItem (idc_static)->setwindowtext (L"Hello Windows" );      }             return 0 ;  }  

6. Implement thread functions and send custom messages via PostMessage

 uint cthreaddemodlg::threadfunction (lpvoid pparam) {cthreaddemodlg  *pdlg =        (Cthreaddemodlg * while   (TRUE) {::P ostmessage (Pdlg - >m_hwnd, wm_update_static, 0 , 0   1000  ->m_hwnd, wm_update_static, 1 , 0  );      Sleep ( 1000   return  0  ; }  

7. Create a thread in the member function OnInitDialog and start

this);   

Update control content by sending a custom message general idea: Create a thread in the main thread of the primary interface---> Bind the thread's starting function to a static member method of the class---> Call PostMessage () or SendMessage () in a static member method Send a custom message---> Update the status display of the controls on the main interface in the message response function of the custom message.   

Original link: Two ways to update the contents of a control in an MFC child thread

Go Two ways to update the contents of a control in an MFC child thread

Related Article

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.