I’m using the following method to show a modeless Message Box.
public void ShowMessageBox(string Message)
{
var thread = new Thread(
() =>
{
MessageBox.Show(Message);
});
thread.Start();
}
The “() => {…}” is something I’ve never seen before. What is the name for this code pattern?
Also, thread.Start starts the thread, and it automatically closes once the “()=>{…}” method completes (when the Message Box is OK’ed), right? If so, can you please point me to some official documentation saying that the thread closes automatically?
Thanks!
It’s the lambda operator, and read as “goes to”. MSDN has a good intro: Lambda Expressions (C# Programming Guide)
One concern with your example is that you’re spinning up a new thread to update the UI, the UI is intrinsically single-threaded, so background updates are generally the wrong thing to do (unless you’re manually/explicitly checking
InvokeRequiredand callingInvoke()as needed.Regarding the UI threading…
In WinForms every
FormorControlis created on a particular thread (the “UI Thread”), and you can think of that thread as owning that control (not exactly correct, but a good way to conceptualize it). Updating the UI from that thread is safe, updating the UI from another thread runs the risk of collisions and corruption and all the usual risks of parallel/async programming.…So… how do you safely update the UI from a background thread without blocking the UI? In short–you can’t–the best you can do is block it for the bare minimum required to update the UI. This is where
InvokeRequiredandInvoke()come in…Here’s a sample: you should be able to drop this into the code-behind of a new form with a button and textbox.
To use:
Try commenting out either the call to
SetTextAsyncSafe()orSetTextAsyncSafe()— running both could confuse you since they won’t necessarily execute in the order they’re called (they’re running async, remember?).Then set a breakpoint on
SetText(). You should see the “safe” call will actually call the method twice–the first call will detectInvokeRequiredand will call the method a 2nd time for the correct thread byInvoke()‘ing to it.You should see an Exception thrown when
SetTextAsyncUnsafe()actually gets to thetextBox1.Text = value;statements. The exception will be anInvalidOperationExceptionwith a message stating “Cross-thread operation not valid” — you can google this term for more details.The code: