472,121 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Thread Message Loop in C#?

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

Nov 1 '06 #1
11 24199
"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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.

--
Adam Clauss
Nov 1 '06 #2
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
Adam Clauss wrote:
"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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.

--
Adam Clauss
Nov 1 '06 #3
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

gilbert@gmail wrote:
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
Nov 1 '06 #4
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

Brian Gideon wrote:
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

gilbert@gmail wrote:
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
Nov 1 '06 #5
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

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

Brian Gideon wrote:
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
Nov 1 '06 #6
Have you tried the MessageQueue class, seems to have detailed examples on
the BOL.

Regards,
John

"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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

Nov 1 '06 #7
Check out ThreadPool.QueueUserWorkItem(...)

Might be what you're looking for. C# has a built in thread pool you can use
in each process.
"gilbert@gmail" <gi************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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

Nov 1 '06 #8
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

Nov 1 '06 #9

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

Nov 1 '06 #10

Bruce Wood wrote:
gilbert@gmail wrote:
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/
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

Nov 1 '06 #11
Brian Gideon <br*********@yahoo.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 4 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Serge | last post: by
3 posts views Thread by Sean | last post: by
2 posts views Thread by Javier Bertran | last post: by
7 posts views Thread by Jeff Stewart | last post: by
reply views Thread by fiefie.niles | last post: by
5 posts views Thread by fniles | last post: by
5 posts views Thread by Alan T | last post: by
2 posts views Thread by Steve | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.