is it safe to call GetMessage instead of calling DoEvents

G

Guest

I am creating a thread via "new Thread(new ThreadStart(p.ThreadProc))", is it
safe for ThreadProc to use GetMessage/TranslateMessage/DispatchMessage
instead of DoEvents?

What I would like to do is to have messages posted to ThreadProc and have
ThreadProc process those messages (similar to using PostThreadMessage in
unmanaged code).

Thanks.
 
J

Jon Skeet [C# MVP]

Benny said:
I am creating a thread via "new Thread(new ThreadStart(p.ThreadProc))", is it
safe for ThreadProc to use GetMessage/TranslateMessage/DispatchMessage
instead of DoEvents?

What I would like to do is to have messages posted to ThreadProc and have
ThreadProc process those messages (similar to using PostThreadMessage in
unmanaged code).

I would suggest using a queue of work items. See
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml
about half way down for a sample producer/consumer queue.
 
G

Guest

Jon,

Thanks for the reply. I will do what is recommended.

For my future reference, is it safe to call unmanaged message APIs instead
of doing DoEvents? Thanks.
 
J

Jon Skeet [C# MVP]

Benny said:
Thanks for the reply. I will do what is recommended.

For my future reference, is it safe to call unmanaged message APIs instead
of doing DoEvents? Thanks.

I wouldn't recommend using DoEvents in the first place. Can you use the
unmanaged APIs? Probably - very carefully. I would try to avoid doing
so though. I've certainly never needed them myself, and I've done a
certain amount of "interesting" threading.
 
J

Jeffrey Tan[MSFT]

Hi blee,

Thanks for your post!

Based on my understanding, you want to know if calling unmanaged message
API in the worker thread is safe, if I misunderstand you, please feel free
to tell me.

I am not sure why you want to call the
GetMessage/TranslateMessage/DispatchMessage API in a worker thread,
normally, these 3 APIs are used to construct a message loop, so that any
Windows message post/send to the thread can be processed correctly.
Normally GUI thread will use these APIs, because GUI thread will create
Window in the thread, then OS code will generate the proper messages for
the window to the GUI thread. Because a worker thread does not have a GUI
window, it will not have any messages in the queue.

However, if you want to use the Message APIs in the worker thread, I did
not see the problem, we can manually create a similar message loop in the
worker thread, then the *client* thread can p/inoke PostThreadMessage to
post the *wake* message to the thread message queue to wake up the
GetMessage API. This should be a way of doing one way cross-thread
communication. However, this kind of task can be easily achieved through
other dedicated thread communication technologies.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top