Suppose I have some information received on asynchronous sockets (therefore coming out of some background threads) that constantly update the form. How can I safely update the contents of a form to avoid an ObjectDisposedException that sometimes occurs when a form is closed?
In the update operation (in the background thread) I check the IsDisposed form property, but this is useless, because the user interface thread sometimes provides the form right after the check and right before the update operation, causing the exception to be thrown (when I close the form) I tried to use the lock on the form object in the onScreenFormClosed form handler and in the update operation to ensure that this operation is not performed at the same time, but it blocks the user interface thread.
I even tried running the update operation on the user interface thread, but that was useless, since invoking Invoke (...) on the form still threw an ObjectDisposedException.
source
share