表单以某种方式被处置

发布于 2024-09-03 03:59:13 字数 727 浏览 10 评论 0原文

我有一个客户端-服务器应用程序,在其中我使用经典的套接字和线程来接收/发送数据并侦听客户端。

应用程序工作正常,但经过一段随机时间后,我得到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2024-09-10 03:59:13

您是否尝试过重写 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.

梦在深巷 2024-09-10 03:59:13

我遇到了类似的问题,我只是隐藏了一个表单,从不关闭它或处置它。

表单被处置的根本原因是取消按钮的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文