In this article:
http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx
The author uses the following method to make thread-safe calls to a Windows Forms control:
private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } }
Is there a shorter way to accomplish the same thing?
C# 3.0 and after:
An extension method would generally be the way to go, since you’re always going to want to perform an action on an
ISynchronizeInvokeinterface implementation, it’s a good design choice.You can also take advantage of anonymous methods (closures) to account for the fact that you don’t know what parameters to pass to the extension method; the closure will capture the state of everything needed.
You’d then call it like this:
Here, the closure is over the
textparameter, that state is captured and passed as part of theActiondelegate passed to the extension method.Before C# 3.0:
You don’t have the luxury of lambda expressions, but you can still generalize the code. It’s pretty much the same, but not an extension method:
And then you call it with anonymous method syntax: