Word from the Author

"One mans mistake is another mans break"
Most of the article(s) which I have posted here are based on my own personal experience. Please do not get aggravated if you disagree with what I wrote.This is just my opinion and it might not be worth much. In case you find some errors or mistakes , have any other useful information which others including myself would benefit from or if you like an article you can always post your messages and comments here.

Interthread Communication using PostThreadMessage in C++

In order to communicate between various running thread we could use the  PostThreadMessage

HANDLE Threadhandle;
DWORD  Threadid;

#define UWM_SEND_MY_STRING (WM_APP + 3) 

DWORD WINAPI Threadproc(LPVOID lpParameter )
{
 MSG msg;
 char *p = new char;

 while(GetMessage( &msg, NULL, 0, 0 ))
 {
 p = (char*)(msg.wParam);
 cout << p;
 }
 return 0;  //Exit the thread
}

int main()
{
MSG msg;
Threadhandle = CreateThread(NULL,0,Threadproc,NULL,0,&Threadid);
char *p=new char;
p="Send Message";       
Sleep( 1000 );
while
(TRUE)
 {
  if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0)
    {
    printf("failure");
    }
   Sleep(1000);
 }
}
 



No comments:

Post a Comment