I’m writing a bit of code that will open a MessageBox on a separate thread to prevent the MessageBox from stopping the program. It is very very important that starting a new thread will not crash the program that I am running, but I don’t know enough about threads to make sure this happens.
My question is, after starting the thread, how can I safely dispose of it after the MessageBox closes? I imagine closing/disposing of it is necessary so it’s not just floating around after it is created and started.
Please advise, thanks!
var Thread = new Thread
(
()=>
{
MessageBox.Show("Buy pizza, Pay with snakes");
}
);
Thread.Start();
The thread will close automatically after the scope of the lambda expression is left… in your case you don’t need to worry about anything.
In general it’s also good practice to set the thread to background, because if your application is closed you might get a message box just hanging out there by itself:
Note: it’s preferred that your variables start with a lower letter. For details on naming conventions please see the Microsoft Naming Guidelines.