Click here to Skip to main content
13,832,412 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 14 Jul 2011
Licenced CPOL

PostThreadMessage Demystified

, 21 Jun 2013
Rate this:
Please Sign up or sign in to vote.
How and Where to use it!


Demystifying the use of much unused and highly advantageous API available for multi-threading!. Code given here is for learning/demo purpose, SHOULDN'T  BE USED DIRECTLY INTO PRODUCTION.  

Using the Code

For years working in multithread environment, I always wonder what's the use of PostThreadMessage? Could it can be utilized in worker thread (though I used the same in UI thread derived from CWinThread).

I am sure every API provided has something for use, otherwise Microsoft programmers are not stupid to provide something which is not working as part of API library. So I created a test program and when playing with worker thread, I provided a message loop inside it with HWND parameter passed as NULL:

// Window MSG Structure
MSG msg;

// Window blocking Message Loop

This means that getmessage can gather message for current thread whether it is thread message or window message. Here is the extract from MSDN.

“If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.”

Now I added a message handler inside message loop which would wait for say thread message (WM_APP+1) to perform some action, otherwise it with continue with its work. Our fully loaded thread function would look something like this:

DWORD threadProc(LPVOID lParam)
	MSG msg;
		if(msg.message == WM_APP+1)
			MessageBoxA(NULL,"Hello","From Thread",MB_OK);

	MessageBoxA(NULL,"Thread will closed On pressing OK","From Thread",MB_OK);
	return 0;

Now, you are still wondering where our PostThreadMessage API is and how to use it? Now PostThreadMessage API works on thread ID instead of HWND or window handle. Here is a small extract of PostThreadMessage from MSDN:

Posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.

BOOL WINAPI PostThreadMessage(
  __in  DWORD idThread,
  __in  UINT Msg,
  __in  WPARAM wParam,
  __in  LPARAM lParam

Now, to get the thread id, when creating thread you have to pass DWORD variable by reference to gather the thread id of newly created worker thread, something like this:

DWORD m_idThread;// in Main Class

Same in _beginthreadex() API, however there is no such arrangement in _beginthread() API to retrieve thread id. Though there are ways, I am not them discussing here, i.e., off topic. Now that you have the thread id of worker thread, using PostThreadMessage you can post message to thread, i.e., for saying hello:


And instructing thread to quit:


Here is how the running application looks like:



Points of Interest

I was more interested in posting it as a tip and tricks; however I thought that posting it as an article would target a larger audience.


  •  21-June-2013 - Updated 
  • 14 July 2011: Posted on CodeProject


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)

He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!

He started with Visual C++ then moved to C# then he become language agnostic, you give him task,tell him the language or platform, he we start immediately, if he knows the language otherwise he quickly learn it and start contributing productively

Last but not the least, For good 8 years he was Visual CPP MSMVP!

You may also be interested in...

Comments and Discussions

QuestionNot much help Pin
Vaclav_21-Sep-13 5:57
memberVaclav_21-Sep-13 5:57 
AnswerRe: Not much help Pin
ThatsAlok22-Sep-13 22:23
memberThatsAlok22-Sep-13 22:23 
GeneralSimilar technique Pin
Manish K. Agarwal24-Jun-13 1:21
memberManish K. Agarwal24-Jun-13 1:21 
AnswerRe: Similar technique Pin
ThatsAlok26-Jun-13 18:11
memberThatsAlok26-Jun-13 18:11 
GeneralRe: Similar technique Pin
ThatsAlok26-Jun-13 18:15
memberThatsAlok26-Jun-13 18:15 
GeneralRe: Similar technique Pin
Manish K. Agarwal26-Jun-13 22:48
memberManish K. Agarwal26-Jun-13 22:48 
SuggestionCareful there! Pin
Pablo Aliskevicius18-Jul-11 21:36
memberPablo Aliskevicius18-Jul-11 21:36 
GeneralRe: Careful there! Pin
ThatsAlok18-Jul-11 23:55
memberThatsAlok18-Jul-11 23:55 
Question[My vote of 1] Breaks the Cardinal Rule of PostThreadMessage Pin
Mike O'Neill18-Jul-11 10:20
memberMike O'Neill18-Jul-11 10:20 
AnswerRe: [My vote of 1] Breaks the Cardinal Rule of PostThreadMessage Pin
ThatsAlok18-Jul-11 23:47
memberThatsAlok18-Jul-11 23:47 

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 | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 21 Jun 2013
Article Copyright 2011 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid