(VC/MFC) passing parameters to a thread through a struct

Source: Internet
Author: User

The following reprint "Gu su old poplar": http://www.cnblogs.com/fdyang/archive/2012/06/15/2858730.html


Pass parameters to the thread through the struct.

Purpose: in a dialog box, click the button to start multiple threads calling external programs (batch), get the return value, and then update the results to multiple edit controls

idea: control data updates in the thread function by passing parameters to the threads through a struct that contains control information.

steps:

1. Create a struct in the header file XXXDlg.h of the dialog class, including a pointer to the control.
struct threadinfo{CEdit *pedit1_1; }


Next, declare the thread function: UINT threadfunc (LPVOID lpparam);

Note that both of the above should be outside the class Cxxxdlg.


2. Define public variables in the XXXDlg.cpp file: (global variable)
Threadinfo Mthreadinfo;

 
3. In the message handler function of the button, create the thread, calling the thread function. (1) Set the value of the instance of Threadinfo .   mthreadinfo.pedit1_1=&edit1_1;   //point to edit control      //(2) Start a thread    cwinthread *pthread=afxbeginthread (    threadFunc,     &mThreadInfo,    THREAD_PRIORITY_NORMAL,    0,     create_suspended  );   pthread->resumethread ();     //(3) Add a threading function outside the class    Uint threadfunc (lpvoid lpparam)    {    threadinfo *pinfo= ( threadinfo*) lpparam;     //A pointer to an instance of the struct body.     //calls the batch handler function and displays the returned result (CString) to the edit  control.     pinfo->pedit1_1->setwindowtext (Docheck (Batfilepath));    return 0;   }  

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.