I have a client-server application, in which I use classic Sockets and threads for receiving/sending data and listening for clients.
The application works fine, but after some random time I get the ObjectDisposedException:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MainForm'.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
That code is called from client Socket thread and I use Invoke() method to ru开发者_开发技巧n the code on UI thread.
I'm sure that I don't manually dispose the form nor using Close() (form is closed by user clicking Close button), so I don't know what could cause its disposing.
Edit: Exception isn't thrown during Form closing - it happens totally randomly.
Have you tried overriding the Dispose
method and putting a break point in there? The callstack would probably give you an indication of why/where it is being disposed.
I had a similar problem, I was just hiding a form and never closing it or disposing it.
The root cause of the form being disposed was due to a DialogResult of a Cancel button that actually was forcing the form Close and then the dispose was implicit.
To overcome this i just removed the DialogResult beaviour from the form.
精彩评论