Thread Message Loop in C#?

G

gilbert@gmail

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
 
A

Adam Clauss

gilbert@gmail said:
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

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.
 
G

gilbert@gmail

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
 
B

Brian Gideon

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
 
G

gilbert@gmail

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
 
J

John J. Hughes II

Have you tried the MessageQueue class, seems to have detailed examples on
the BOL.

Regards,
John
 
B

Brian Gideon

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
 
J

Jim H

Check out ThreadPool.QueueUserWorkItem(...)

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

gilbert@gmail

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
 
B

Bruce Wood

gilbert@gmail said:
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!

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/
 
B

Brian Gideon

Bruce said:
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/

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
 
J

Jon Skeet [C# MVP]

Brian Gideon said:
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" :)

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...
 

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