Archive for January 7th, 2008
Free Asynchronous Message Queue
by brian on Jan.07, 2008, under GUI, c#, c# coding GUI, coding
If you’ve ever used a message queue to talk to different parts of your application you know that sometimes you run into a problem with tangled call stacks; especially if one message can causes another message to be sent.
However, in c# .Net there is a simple way to untangle the call stack of the message queue. Just use the BeginInvoke method. This unwinds the stack and uses a delegate to call your message reception handler from the control’s thread.
For instance:
delegate void PostOfficeMessageHandler( POMessage message );
public void SendMessage(Control Sender, POMessage Message)
{
PostOfficeMessageHandler mh = recipients[Sender];
Sender.BeginInvoke(mh, Message);
}
The advantages of this method of deliver are two fold. 1. The stack is unwound. 2. It makes message handler in the control’s threads solving some threading issues.
There are a couple of disadvantages. 1. Extra overhead. 2. If the message causes problems you may not know where it’s coming from!
