The correct way to update the user interface on another topic

So, I have this simple class that updates my labels, and they are accessed by various threads and progress reports of my application. It works fine, but when you close this application, this code always gives an error about trying to access what is located.

private delegate void SetLabelTextDelegate(string str1, string str2); public void SetLabelText(string str1, string str2) { if (this.label1.InvokeRequired || this.label2.InvokeRequired) { this.Invoke(new SetLabelTextDelegate(SetLabelText), new object[] { str1, str2}); return; } this.label1.Text = (str1 == string.Empty) ? this.label1.Text : str1; this.label2.Text = (str2 == string.Empty) ? this.label2.Text : str2; } 

Isn't that the right way? Is there something I need to add to make sure that it is not trying to perform updates in the user interface when the application closes?

+6
source share
1 answer

The ObjectDisposedException event you received is most likely due to the fact that Form closes when Invokes (in the queue) is not yet completed. You need to either allow Invokes to complete before allowing closure of the form, or you will have to handle an ObjectDisposedException.

Cm:

+1
source

All Articles