[MFC] multi-task threads

Source: Internet
Author: User
Instance -- multitestthread

Key header file code:

 

// Multitestthreaddlg. h: header file # define wm_msg wm_user + 1 class program; typedef struct thread_param {cmultitestthreaddlg * pdlg; int nindex;} _ thread_param; uint threadfun (lpvoid pparam );

 

 

Key code of the CPP file:

 

// Multitestthreaddlg. CPP: implementation file cmultitestthreaddlg: cmultitestthreaddlg (cwnd * pparent/* = NULL */): cdialogex (cmultitestthreaddlg: IDD, pparent) {m_hicon = afxgetapp () -> loadicon (idr_mainframe); // member variable // cwinthread * pthread1; // cwinthread * pthread2; // thread_param mthreadparam; // cprogressctrl break; pthread1 = NULL; pthread2 = NULL; mthreadparam. pdlg = NULL;} void cmultites Tthreaddlg: dodataexchange (cdataexchange * PDX) {// member variable // cwinthread * pthread1; // cwinthread * pthread2; // thread_param mthreadparam; // cprogressctrl mpgsctrlone; // cprogressctrl forward; cdialogex: dodataexchange (PDX); ddx_control (PDX, idc_progress_one, mpgsctrlone); ddx_control (PDX, progress, latency);} forward (Direction, cdialogex) on_wm_syscommand () on_wm_paint () on _ Wm_querydragicon () on_bn_clicked (response, & cmultitestthreaddlg: Response) on_bn_clicked (response, & Response: Response) on_bn_clicked (response, & Response: Response) // on_message (wm_msg, & cmultitestthreaddlg: onmsgfun) // replace end_message_map () // Rewrite cdialogex: windowproc () message function lresult cmultitestthreaddlg: windowproc (uint message, wparam, lparam) {If (Message = wm_msg) {If (wparam = 1) {mpgsctrlone. setpos (lparam);} else if (wparam = 2) {mpgrsctrltwo. setpos (lparam) ;}} return cdialogex: windowproc (message, wparam, lparam); // note the cdialogex} // wm_msg message function, on_message (wm_msg, & cmultitestthreaddlg :: onmsgfun) lresult cmultitestthreaddlg: onmsgfu N (wparam, lparam) {If (wparam = 1) {mpgsctrlone. setpos (lparam);} else if (wparam = 2) {mpgrsctrltwo. setpos (lparam) ;}return 0 ;}// the start button 1 button void cmultitestthreaddlg: onbnclickedbuttonstart1 () {If (pthread1! = NULL) {DWORD exitcode = 0; If (: getexitcodethread (pthread1-> m_hthread, & exitcode) {If (exitcode = still_active) {afxmessagebox (L "thread 1 has been started. "); Return ;}} mthreadparam. nindex = 1; mthreadparam. pdlg = This; pthread1 = afxbeginthread (threadfun, & mthreadparam);} // start thread 2 button void cmultitestthreaddlg: onbnclickedbuttonstart2 () {If (pthread2) {DWORD exitcode = 0; if (: getexitcodethread (pthread2-> m_hthread, & exitcode) {If (exitcode = still_active) {afxmessagebox (L "thread 2 has started. "); Return ;}} mthreadparam. nindex = 2; mthreadparam. pdlg = This; pthread2 = afxbeginthread (threadfun, & mthreadparam); // pthread2-> m_bautodelete = false; // when it is false, in the destructor, waitforsingleobject () Deletion thread} // reset button event void cmultitestthreaddlg: onbnclickedbuttonreset () {// terminate thread if (pthread1) {terminatethread (pthread1-> m_hthread, null );} if (pthread2) {terminatethread (pthread2-> m_hthread, null);} // reset the progress bar mpgsctrlone. setrange (0,100); mpgsctrlone. setpos (0); mpgrsctrltwo. setrange (0,100); mpgrsctrltwo. setpos (0);} // The thread function in the format of uint fun (lpvoid pparam) uint threadfun (lpvoid pparam) {thread_param * pthreadparam = (thread_param *) pparam; for (INT I = 0; I <1001; I ++) {if (I % 10 = 0) // send a message when it is a multiple of 10, update the corresponding progress bar {sleep (1); // Delay Time: postmessage (pthreadparam-> pdlg-> m_hwnd, wm_msg, pthreadparam-> nindex, I/10 );}} return 0 ;}

Structure:

 

 

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.