A quick question about concurrency in C#. I have in my code the following method to update a textbox:
public void diag(string text)
{
if (InvokeRequired)
{
Invoke(new Action<string>(diag), text);
}
else
{
tbDiag.Text += text;
}
}
This method gets called in any number of methods in my program to report stuff. Among them, it gets called in the ReportProgress of a BackgroundWorker: this happens periodically every few tens of milliseconds. If I close the form (and the application) without stopping the BackgroundWorker, i get a System.InvalidOperationException because the TextBox has been disposed.
Is there a way to prevent this from the handler for the FormClosing event? Either cancelling the running delegate or waiting for its completion would be fine, just as long as I can get to prevent that exception being thrown. What I would like to avoid is having to keep a direct reference to the delegate.
I apologize if this was answered already somewhere else. I have been googling this to no avail.
Thank you.