Groups | Blog | Home
all groups > c# > october 2006 >

c# : Thread Message Loop in C#?


gilbert NO[at]SPAM gmail
10/31/2006 9:58:41 PM
Hello!

I am writing an application that would like to make use of a thread
message
queue, like the one provided in MFC. In MFC, the message poster calls
the
function PostThreadMessage. The receiving thread receives the message
and
process it. Therefore, the caller and callee thread are different. I
would
like to know if there is any build-in mechanism in C#? Or I have to
implement the whole stuff by myself?

Thank you very much!
Gilbert
gilbert NO[at]SPAM gmail
11/1/2006 5:18:36 AM
Thank you for your response. However, it is not a windows form object.
It is a thread object. I have been reading the beginInvoke function of
a delegate. However, it seems it just puts the work onto a thread in
the system thread pool. I wonder if there is a functionality that I can
post a message to a thread I created and a message loop of that thread
would process my message. Thank you very much!

Gilbert
[quoted text, click to view]
Brian Gideon
11/1/2006 5:33:17 AM
Gilbert,

You can call Application.Run to install a windows message loop on the
current thread. The PostThreadMessage or Control.BeginInvoke (if you
have a control hosted on the thread) can be used to dispatch messages
to the queue.

If you don't want the message queue to receive windows messages then
you'll have to write your own.

Brian

[quoted text, click to view]
Adam Clauss
11/1/2006 6:29:18 AM
[quoted text, click to view]

Assuming you are making some form of WinForms app, you can call Invoke() on
your form. That will effectively put the delegate you pass into the message
queue of the form and the delegate will be called on the form's thread
rather than the calling thread.

--
Adam Clauss

gilbert NO[at]SPAM gmail
11/1/2006 7:29:41 AM
Thank you very much for your tips. After searching from the web, I
still have no idea how to use the message loop created by the
Application.Run(). Like, how to put the custom message handler? How to
post custom messages to the loop? I think it is the thing I am looking
for, just I still have no idea how to use it!

Any help would be greatly appreciated!
Thanks!

Gilbert

[quoted text, click to view]
Brian Gideon
11/1/2006 9:05:20 AM
Gilbert,

Here's an example.

public class Program
{

public static void Main()
{
// Get RunMessageLoop going in another thread.
}

private void RunMessageLoop()
{
Application.AddMessageFilter(new MyMessageFilter());
Application.Run();
}

private class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == /* whatever */)
{
// Intercept and handle accordingly.
return true;
}
// Allow the message loop to dispatch the message.
return false;
}
}
}

Brian

[quoted text, click to view]
John J. Hughes II
11/1/2006 11:56:43 AM
Have you tried the MessageQueue class, seems to have detailed examples on
the BOL.

Regards,
John

[quoted text, click to view]

gilbert NO[at]SPAM gmail
11/1/2006 11:58:59 AM
Thank you very much for all of these tips. I think the MessageQueue
class is for messaging with MSMQ? I would try to check out the
Application.Run() method. I hope it is not an overkill as I just wanna
send some strings to some specific threads. I am trying to implement
the ActiveObject such that messages passing between threads are queued
up. The calling thread and called thread are decoupled where the
calling thread need not to know the status of the called thread. Thank
you very much for your helps!

Gilbert
Bruce Wood
11/1/2006 12:46:36 PM

[quoted text, click to view]

Have you seen Jon Skeet's excellent writeup on threading? I believe
that he has an example of writing your own queue in there:

http://www.yoda.arachsys.com/csharp/threads/
Brian Gideon
11/1/2006 1:24:40 PM

[quoted text, click to view]

Yep, jump to chapter 4 and look for the ProducerConsumer class. Jon,
have you considered changing your page so that the name of the class is
BlockingQueue instead? It would use Enqueue and Dequeue method names
instead of Produce and Consume. Though, Java's BlockingQueue uses put
and take as method names. The BlockingQueue nomenclature just seems to
be more..."academic" :)

Brian
Jim H
11/1/2006 2:33:14 PM
Check out ThreadPool.QueueUserWorkItem(...)

Might be what you're looking for. C# has a built in thread pool you can use
in each process.


[quoted text, click to view]

Jon Skeet [C# MVP]
11/4/2006 12:00:00 AM
[quoted text, click to view]

I'll add it to the wishlist and think about it some more. I think
having the methods called "Produce" and "Consume" make it more obvious
why the general pattern is called a producer-consumer...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button