?
Solved

PostThreadMessage and PeekMessage

Posted on 1999-10-27
5
Medium Priority
?
1,098 Views
Last Modified: 2012-06-27
I've written the code using VC++5.0 in WinNT as follow:

...
void main() {
...
   ft = (HANDLE) _beginthreadex(NULL, sz, POne, 0, 0,(unsigned int*) &dft);
   r = PostThreadMessage(dft, WM_NULL, 0,0);
   Sleep(5000);
...
}

void POne() {
...
   MG msg;
   for(;;) {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      (
         printf("Message Received!\n");
         break;
      }
      WaitMessage();
   }
...
}

In running the above code, the 'Message Received' does not prompt. But it work on Win95 successfully. I've no idea about this problem. Does any suggestion or advice for me?
Thank you for your attention.

Regards,
Michael

0
Comment
Question by:mikehui
  • 2
  • 2
5 Comments
 

Author Comment

by:mikehui
ID: 2164810
Edited text of question.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2165349
What messages should it be geting?  probably none.   You might try using AttachThreadInput() so that it gets some of the input messages that would be going to the main thread.

What is it that you are trying to do?
0
 
LVL 3

Accepted Solution

by:
PinTail earned 200 total points
ID: 2165644
Have you vaildated the return from _beginthreadex, and PostThreadMessage.

Also, you ought to Sleep in between the call to _beginthreadex and the call to PostThreadMessage, this gives the thread a chance to create it's thread message queue, which it will do upon the first call to PeekMessage.

A Sleep(1) is sufficient, however a better solution is to have a mutex, or some other synchronization object.  Have the thread signal the object after it's first call to PeekMessage(), this then allows your creating thread ( main() ) to know when the thread has activated the queue.

Remember that Win9X is still not a true pre-emptive multi-threaded environment.
0
 

Author Comment

by:mikehui
ID: 2166133
Does any time interval required in a sequence of  PostThreadMessage function call?
0
 
LVL 3

Expert Comment

by:PinTail
ID: 2168124
Do you mean 'Is there a time interval required between calls to PostThreadMessage'.

No, the Threads Message Queue will continue to accept messages as fast as you can post them, however, you should check the return from PostThreadMessage, since it can sometimes fail - for reasons unclear -
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question