Click here to Skip to main content
13,042,166 members (67,295 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 30 Jul 2004

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:


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

 afx_msg void OnRun(WPARAM wParam,LPARAM lParam); 



BEGIN_MESSAGE_MAP(PlaySound1, CWinThread)
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;
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 about project.


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

India India
I am 20 yrs old and a student at Institute of Technology and Management College ( 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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
PGr_16-Jun-09 5:19
memberPGr_16-Jun-09 5:19 
QuestionHow To Set The Facecolor for the scrollbar Pin
N.NagaMahesh1-Nov-07 21:48
memberN.NagaMahesh1-Nov-07 21:48 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 31 Jul 2004
Article Copyright 2004 by xsamar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid