表单以某种方式被处置
我有一个客户端-服务器应用程序,在其中我使用经典的套接字和线程来接收/发送数据并侦听客户端。
应用程序工作正常,但经过一段随机时间后,我得到 ObjectDisposeException:
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)
该代码是从客户端 Socket 线程调用的,我使用 Invoke() 方法在 UI 上运行代码线。
我确信我不会手动处置表单,也不会使用 Close() (表单由用户单击“关闭”按钮关闭),所以我不知道什么会导致其处置。
编辑:在表单关闭期间不会抛出异常 - 它完全随机发生。
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 run 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过重写 Dispose 方法并在其中放置断点?调用堆栈可能会告诉您它被处置的原因/位置。
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.我遇到了类似的问题,我只是隐藏了一个表单,从不关闭它或处置它。
表单被处置的根本原因是取消按钮的 DialogResult 实际上强制表单关闭,然后隐式处置。
为了克服这个问题,我只是从表单中删除了 DialogResult 行为。
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.