CWinThread class usage + examples

Source: Internet
Author: User

CWinThread There are two usages, one of which is worker thread, also called worker thread ( Worker_thread ) , the other is an interactive thread ( User_interface_thread ). The first is simple, and the second one is relatively complex. Let's take a concrete look. (the author is using VC2010)

Example one:

We build a Win32 Console program Empty project, right click on the project, select "Properties",

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/86/wKiom1VLCzHA76uCAAMetuahrmk862.jpg "title=" 1.png " alt= "Wkiom1vlczha76ucaametuahrmk862.jpg"/>

Make changes to the project as above.

Enter the following code:

#define _afxdll#include "AFXWIN.h" Class Cmythread:public Cwinthread{public:declare_dyncreate (cmythread) Protected: Virtual BOOL InitInstance ();};i Mplement_dyncreate (Cmythread,cwinthread) BOOL cmythread::initinstance () {//Here you can add threading code, you can do whatever you want to do here! _tprintf (L "Hello CWinThread as Worker thread\n");//Note that this must return false, which is different from the place of User_interface_thread! If this returns//TRUE, then after the InitInstance function is executed, the MFC framework will invoke the virtual function of the Cmythread class. The run ()//run function executes a message loop, in a worker thread, without a window, or a message loop. So be sure to return here false,//remember!!! return FALSE;} int _tmain () {//Create a new thread cwinthread* p=afxbeginthread (Runtime_class (Cmythread)); _gettchar (); return 0;}



The problem that should be noticed in this is already given in the notes of the program. using this class in real MFC, there is no difference in usage, we focus on the usage of user_interface_thread.

Example two:

first create a MFC Program, select the dialog box program to complete the direct point.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/86/wKiom1VLC3qCaa48AAH8V5Qjmng543.jpg "style=" float: none; "title=" 2.PNG "alt=" Wkiom1vlc3qcaa48aah8v5qjmng543.jpg "/>


In this way, the system will generate a dialog box for us to directly modify the dialog resource to look like this:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/86/wKiom1VLC3yy20L_AACe00-YeAs071.jpg "style=" float: none; "title=" 4.PNG "alt=" Wkiom1vlc3yy20l_aace00-yeas071.jpg "/>


Double click "OK" to get

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/82/wKioL1VLDO_D8pLdAACP_h4VVHI122.jpg "style=" float: none; "title=" 5.PNG "alt=" Wkiol1vldo_d8pldaacp_h4vvhi122.jpg "/>

Click on the "Project" menu and select "Add Class"

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/86/wKiom1VLC37xiUQ6AAKpsUmsxvA685.jpg "title=" 6.PNG " alt= "Wkiom1vlc37xiuq6aakpsumsxva685.jpg"/>

Select MFC class.



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/82/wKioL1VLDPKx_kiRAAGcaqt_E7E524.jpg "style=" float: none; "title=" 7.PNG "alt=" Wkiol1vldpkx_kiraagcaqt_e7e524.jpg "/>


"class name": Cmythread "base class name" : CWinThread "Done" directly.

Automatic System Generation Cmythread class, altogether two files MyThread.h and the MyThread.cpp

in the MyThread.cpp in which Add the following code:

 MyThread.cpp :  implement file//#include   "stdafx.h" #include   "TT,. h" #include   "MyThread.h"  cmythreadlresult callback mainwndproc (    HWND hwnd,         // handle to window    uint umsg,         // message identifier    wparam  wparam,    // first message parameter    lparam  lparam)     // second message parameter{       switch  (umsg)      {          case wm_create:             //  initialize the window.              Return 0;          case wm_paint:              // paint the window ' s client area.              return 0;           case WM_SIZE:              // set the size and position of the window.              return 0;           case WM_DESTROY:              // Clean up window-specific data objects.  PostQuitMessage (0);            return 0;      &nbSp;    //         // process other  messages.         //           default:              Return defwindowproc (Hwnd, umsg, wparam, lparam);     }      return 0; } // cmythreadimplement_dyncreate (CMyThread,  CWinThread) Cmythread::cmythread () {}cmythread::~cmythread () {}bool cmythread::initinstance () {// TODO:   Perform arbitrary thread-by-line initialization here Wndclass wc,wc1;      // register the main  window class.     wc.style = CS_HREDRAW | CS_VREDRAW;      wc.lpfnWndProc =  (WNDPROC)  MainWndProc;      wc.cbclsextra = 0;     wc.cbwndextra = 0; wc.hinstance = :: GetModuleHandle (NULL);      wc.hicon = loadicon (null, idi_application);      wc.hcursor = loadcursor (Null, idc_arrow);      wc.hbrbackground = (Hbrush) getstockobject (White_brush);      wc.lpszmenuname  =null;     wc.lpszclassname = l "YDM";   if (! GetClassInfo (GetModuleHandle (NULL), L "YDM", &WC1)) {if  (! RegisterClass (&WC))  return false; }hwnd hwnd=createwindow (L "YDM", NULL,WS_ Overlappedwindow,0,0,500,300,null,null,::getmodulehandle (NULL), NULL); ShowWindow (hwnd,sw_show); UpdateWindow (HWND); return true;} Int cmythread::exitinstance () {// todo:  performs any thread-wise cleanup return cwinthread::exitinstance () here; Begin_message_map (Cmythread, cwinthread) end_message_map ()// cmythread  message handlers 

in cpp

 TT, .cpp :  defines the class behavior of the application. #include   "stdafx.h" #include   "TT,. h" #include   "TT, Dlg.h" #ifdef  _debug#define new  DEBUG_NEW#ENDIF// CTT, Appbegin_message_map (Ctt, App, cwinapp) on_command (ID_HELP, & CWINAPP::ONHELP) End_message_map ()// CTT, app  construction Ctt, APP::CTT, APP () {//  support Restart Manager m_ dwrestartmanagersupportflags = afx_restart_manager_support_restart;// todo:  Add the Construction code here,//   Place all important initializations in  InitInstance }//  only one  ctt, app  object Ctt, APP THEAPP;// CTT, app  Initialize BOOL CTT, app::initinstance () {//  If an application manifest that runs on  Windows XP  specifies that you want to use//   ComCtl32.dll  version  6  or later to enable visualization,//Requires  initcommoncontrolsex (). Otherwise, the window cannot be created. Initcommoncontrolsex initctrls;initctrls.dwsize = sizeof (initctrls);//  set it to include all the   Common control classes. Initctrls.dwicc = icc_win95_classes;initcommoncontrolsex (&initctrls); CWinApp::InitInstance ();AfxEnableControlContainer ();//  create  shell  Manager in case the dialog box contains//  any  shell  treeview control or  shell   List view Control. cshellmanager *pshellmanager = new cshellmanager;//  Standard initialization//  If these features are not used and you want to reduce//   The size of the final executable file, you should remove the following//  specific initialization routines//  change the registry key used to store the settings// todo:  the string should be modified appropriately,//  For example, modify the company or organization name Setregistrykey (_t ("Local application generated by Application Wizard")); Ctt, Dlg dlg;m_pmainwnd = &dlg;int_ptr nresponse = dlg. DoModal ();if  (Nresponse == idok) {// todo:  This place code that handles when to close a dialog box with//   "OK"}else  if  (Nresponse == idcancel) {// todo:  in this place code that handles when to close the dialog box with//   "Cancel"}//   Delete the  shell  manager created above. if  (pshellmanager != null) {Delete pshellmanager;}   Because the dialog box is closed, it returns  FALSE  to exit the application,//   instead of launching the application's message pump. Return false;} Compile and Execute, click "OK" on the dialog, and a new dialog box will appear. This is how the User_interface_thread is used. We can replace the bold code in the above two code with any code we want.


This article is from the "C" blog, please be sure to keep this source http://5412097.blog.51cto.com/5402097/1643861

CWinThread class usage + examples

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.