Archive for January 3rd, 2008
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.
