• 0

[C#] Alredy Disposed


Question

I have a main form with the UI, and a separate thread which will "safely" write messages to the UI.

The problem arises when the main form shuts down, its possible the thread has already started to write another message so it will cause an already disposed exception when it access the main form. I modified this so it checks if the main form has been disposed or not before it writes the message. Now however there is an even rarer condition where the main form is disposed between the thread checking that the main form has not been disposed and writing the message.

Is there anyway round this?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Add an form_closing event and check if the thread is still running. If it is, cancel the form close, end the thread and then close the form when the thread finishes. Wrapping code in try / catch by itself will solve the problem but isn't the best coding practice.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.