473,383 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 24390
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
3
by: Sean | last post by:
Hi all, I have a few questions here regarding UI thread. (1) If I have two windows A and B, then I call B.show() in A. In this case, there will be only 1 UI thread which is the window A UI...
2
by: Javier Bertran | last post by:
Hi all, I have an ActiveX control developed in Visual C++ 6.0 that I want to use in a C# project. I want the ActiveX code to run on a separate thread, but can't seem to get it to work. If for...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
5
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
8
by: mtsweep | last post by:
Hi, I started a background thread to preform some time intensive tasks for a GUI frontend. This background thread uses a C++ object which requires a windows message loop so I started one in it...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.