c# coding GUI
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!
Thread Safe calling to a form
by brian on Jan.03, 2008, under .NET, c#, c# coding GUI, coding
Here is an example of a thread-safe call into a form.
delegate void logCallback(string strLog);
private void log(string log)
{
if (this.InvokeRequired)
{
logCallBack d = new logCallback(log);
this.Invoke(d, log);
}
else
{
this.log( strLog );
}
}
First we determine whether the overhead of an invoke can be dismissed. Then if we must invoke we first create an instance of the delegate. Then we call invoke. That’s it. Not too hard.
