我们在尚未处理的表单上收到ObjectDisposedException
from 调用。Invoke
这是一些演示问题的示例代码:
public partial class Form2 : Form
{
void Form2_Load(object sender, EventArgs e)
{
// Start a task that does an Invoke on this control
Task.Factory.StartNew(TaskWork);
// Sleep here long enough to allow the task that does the Invoke
// to execute to the point where it has:
// a. Posted the message and
// b. is waiting
Thread.Sleep(500);
// Cause ShowDialog to return by setting the DialogResult
DialogResult = DialogResult.OK;
}
void TaskWork()
{
// This call doesn't return, but instead throws an ObjectDisposedException
this.Invoke((MethodInvoker)(() => MessageBox.Show("Invoke succeeded")));
}
}
这是我从不关闭的 Form1(主窗体)的调用代码:
public partial class Form1 : Form
{
Form2 m_form2 = new Form2();
void Form1_Load(object sender, EventArgs e)
{
// Call ShowDialog, but don't dispose it.
m_form2.ShowDialog();
// Cause the finalizers to run. This causes an AggregateException to be thrown
// due to the unhandled ObjectDisposedException from the Task.
GC.Collect();
}
}
我们深入研究了 Microsoft 源代码,发现异常是在调用 DestroyHandle 期间创建的(如下)。在完成时,ShowDialog 正在调用 DestroyHandle。
来自 source.NET\4\DEVDIV_TFS\Dev10\Releases\RTMRel\ndp\fx\src\WinForms\Managed\System\WinForms\Control.cs\1305376\Control.cs:
protected virtual void DestroyHandle() {
// ...
// If we're not recreating the handle, then any items in the thread callback list will
// be orphaned. An orphaned item is bad, because it will cause the thread to never
// wake up. So, we put exceptions into all these items and wake up all threads.
// If we are recreating the handle, then we're fine because recreation will re-post
// the thread callback message to the new handle for us.
//
if (!RecreatingHandle) {
if (threadCallbackList != null) {
lock (threadCallbackList) {
Exception ex = new System.ObjectDisposedException(GetType().Name);
while (threadCallbackList.Count > 0) {
ThreadMethodEntry entry = (ThreadMethodEntry)threadCallbackList.Dequeue();
entry.exception = ex;
entry.Complete();
}
}
}
}
// ...
}
问题:
为什么 ShowDialog 会破坏句柄(当我可能重新使用此表单时)?
为什么我得到一个 ObjectDisposedException - 它非常具有误导性(因为它没有被处理)。就好像代码期望只有在对象被释放时才会销毁句柄(这是我所期望的)。
这应该有效吗?也就是说,是否应该允许我在 ShowDialog 之后调用控件?
笔记:
再做一次
.ShowDialog()
会导致创建一个新句柄。如果在
.ShowDialog()
我尝试执行操作之后Invoke
,我会收到一个 InvalidOperationException,指出“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。”如果在完成后
.ShowDialog()
我访问该Handle
属性,然后执行一个Invoke
,它将成功。