Click here to Skip to main content
Click here to Skip to main content

Post a Thread Message and also handle it in the simplest way

, 30 Jul 2004
Rate this:
Please Sign up or sign in to vote.
The message posted to a thread can be easily handled by PeekMessage loop in a similar way as it is done in a window's message loop.

Download all the Zip files, recommended.

ThreadMSG dialog looks some what like this:

The Thread starting with AfxBeginThread dialog looks like this:

Description

This little software does noting but start a Thread, and the title bar blinks to show that it is running. Although a number of threads can be started, in this program that feature has not been stressed. It only handles MSG messages that are send to it or posted to it. One way of doing it can be deriving a class from CWinThread using its normal message maps.

class Thread:public CWinThread
{DECLARE_DYNCREATE(Thread)

public:.... 
 afx_msg void OnRun(WPARAM wParam,LPARAM lParam); 

 DECLARE_MESSAGE_MAP()

};
IMPLEMENT_DYNCREATE(PlaySound1, CWinThread)

BEGIN_MESSAGE_MAP(PlaySound1, CWinThread)
    ON_THREAD_MESSAGE(WM_ON_RUN,OnRun)
END_MESSAGE_MAP()
void OnRun(WPARAM wp,LPARAM lp) 
{
 .....
 .....
}
////To start a thread use the following lines anywhere
////Preferably use OnInitDialog()for general purpose
////first declare an instance of the class Thread with
////the visiblity mode of your preference 
BOOL CThreadMSGDlg::OnInitDialog()
{Thread *pThread;
pThread=new Thread;
pThread->CreateThread();
play->PostThreadMessage(WM_ON_RUN,0,0);
...
...
return TRUE;
}
/*///#///Note: Do not use the overloaded Run function of CWinThread

 it wont let you use ON_THREAD_MESSAGE */

But here, I am using a trick. No need to derive a class from CWinThread. But first download the source code, and then you will find the following methods being implemented there:

  1. First method: The Thread is being started using API function CreateThread(....). This also takes a parameter as a pointer to its threadID and returns the thread ID. This ID is referred to while posting messages by PostThreadMessage(...) in a very similar way. _beginthread(....) function can be used which also gives us the threadID.
  2. Second method: The Thread can also be started using (Preferred in MFC based programs) AfxBeginThread(ThreadProc,lpvoidParam). What it returns is the pointer to a CWinThread object. And there is a public member variable in it: m_nThreadID. So, it also gets initialized by this function. This can be used to post messages to it.

Use PeekMessage to see if there is any message, because this function returns if there is no message, while GetMessage waits infinitely until there is no message. This can lead to blocking of the thread. So take care.

If you have any problems or any queries, feel free to write to me at mailto:sammyitm@hotmail.com?subject=query about project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

xsamar

India India
I am 20 yrs old and a student at Institute of Technology and Management College (www.itmindia.edu) in India.I am doing electronics and communication engineering. My main interests are in computer programming (DirectX,voice,internet srevers and client. Apart from these I have also interest in electronics. I recently made a project using microcontroller(PSoC) and data transfer mechanism using transistor and also in DTMF. I would like to make projects that use electronics as well as computer programming.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberPGr_16-Jun-09 5:19 
QuestionHow To Set The Facecolor for the scrollbar PinmemberN.NagaMahesh1-Nov-07 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 31 Jul 2004
Article Copyright 2004 by xsamar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid