Multi-thread topic-MFC Creation thread (2)

Source: Internet
Author: User

Now, let's write a thread with a message loop.

1. derive your own class (cafxthread) from cwinthread ):

Class cafxthread: Public cwinthread
{
Declare_dyncreate (cafxthread)
Protected:
Cafxthread (); // protected constructor used by Dynamic Creation

// Attributes
Public:

// Operations
Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cafxthread)
Public:
Virtual bool initinstance ();
Virtual int exitinstance ();
//} Afx_virtual

// Implementation
Protected:
Virtual ~ Cafxthread ();

// Generated message map Functions
// {Afx_msg (cafxthread)
// Note-The classwizard will add and remove member functions here.
Afx_msg void onafxtest1msg (wparam, lparam );
Afx_msg void onafxtest2msg (wparam, lparam );
Afx_msg void onafxhellomsg (wparam, lparam );
//} Afx_msg

Declare_message_map ()
};

Cafxthread: cafxthread ()
{
}

Cafxthread ::~ Cafxthread ()
{
}

Bool cafxthread: initinstance ()
{
// Todo: Perform and per-thread initialization here
/*
// Implement the user interface thread
Cframewnd * pwnd = NULL;

Pwnd = new cframewnd;
If (pwnd)
{
Pwnd-> Create (null, "UI thread window ");
Pwnd-> showwindow (sw_show );
Pwnd-> updatewindow ();
M_pmainwnd = pwnd;
}
Return true;
*/
/* Cmultithreaddlg DLG;
M_pmainwnd = & DLG;

DLG. domodal ();
 
Return false ;*/
Return true;
}

Int cafxthread: exitinstance ()
{
// Todo: Perform any per-thread cleanup here
Afxmessagebox (_ T ("Oh! I know it! Bye! "));
Return cwinthread: exitinstance ();
}

Begin_message_map (cafxthread, cwinthread)
// {Afx_msg_map (cafxthread)
// Note-The classwizard will add and remove mapping macros here.
On_thread_message (wm_afx_test1_msg, onafxtest1msg)
On_thread_message (wm_afx_test2_msg, onafxtest2msg)
On_thread_message (wm_afx_hello_msg, onafxhellomsg)
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Cafxthread message handlers

Void cafxthread: onafxtest1msg (wparam, lparam)
{
MessageBox (null, _ T ("Hello world! "), _ T (" test "), mb_ OK );
}

Void cafxthread: onafxtest2msg (wparam, lparam)
{
MessageBox (null, _ T ("Hello world! "), _ T (" test "), mb_ OK );
}

Void cafxthread: onafxhellomsg (wparam, lparam)
{
Afxmessagebox (_ T ("Hello! I'm a thread! "));
: Postthreadmessage (afxgetapp ()-> m_nthreadid, wm_afx_hello_msg, m_nthreadid, 0 );
}

2. Customize the message macro:

# Define wm_afx_test1_msg wm_user + 100
# Define wm_afx_test2_msg wm_user + 101
# Define wm_afx_hello_msg wm_user + 102

3. Let's see how to create a thread:

# Include "afxthread. H"
Void cmultithreaddlg: onbtnusagefour ()
{
// Todo: add your control notification handler code here
// Threads with no interface but message Loops
Cwinthread * pthread = NULL;
 
Pthread = afxbeginthread (
Runtime_class (cafxthread), // Thread class
Thread_priority_normal, // thread priority
0, // in windows, the thread stack size is generally 1 MB. The number of threads created is related to the physical memory and stack space size.
0, // indicates the thread creation flag, such as create_suincluded.
Null); // System Security description, null
 
If (pthread)
{
Pthread-> postthreadmessage (wm_afx_test1_msg, 0, 0 );
Sleep (2000); // if this parameter is removed, the message is lost. Haha, don't believe it. Try it!
Pthread-> postthreadmessage (wm_afx_test2_msg, 0, 0 );
//: Postthreadmessage (pthread-> m_nthreadid, wm_afx_test2_msg, 0, 0 );
// Both methods can send messages to the thread.
/*---------------------------------------------------------------------------*/
Call postthreadmessage.
If it fails, call the sleep function and call postthreadmessage again.
Repeat until postthreadmessage succeeds.
/*---------------------------------------------------------------------------*/

Sleep (2000 );
Pthread-> postthreadmessage (wm_quit, 0, 0); // exit the thread
}
}

Void cmultithreaddlg: onbtnusagefive ()
{
// Todo: add your control notification handler code here
// There are interface threads. Of course, there are also Message loops.
/*
Afxbeginthread (
Runtime_class (cafxthread), // Thread class
Thread_priority_normal, // thread priority
0, // in windows, the thread stack size is generally 1 MB. The number of threads created is related to the physical memory and stack space size.
0, // indicates the thread creation flag, such as create_suincluded.
Null); // System Security description, null
// The message loop will automatically exit with the window closed and clear its own objects
*/

/*
Cafxthread mythread;
Mythread. createthread ();
// This cannot be done...
*/
}

Oh, isn't it so nice? What's next! Haha

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.