我有一个客户端-服务器应用程序,在其中我使用经典的套接字和线程来接收/发送数据和侦听客户端。
该应用程序工作正常,但经过一些随机时间后,我得到了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)
该代码是从客户端 Socket 线程调用的,我使用Invoke()方法在 UI 线程上运行代码。
我确定我没有手动处理表单,也没有使用Close()(用户单击关闭按钮关闭表单),所以我不知道是什么导致它被处理。
编辑:在表单关闭期间不会抛出异常 - 它完全随机发生。