We get an ObjectDisposedException from calling Invoke in a form that has not yet been deleted. Here is a sample code demonstrating the problem:
public partial class Form2 : Form { void Form2_Load(object sender, EventArgs e) {
Here's the call code from Form1 (the main form), which I never close:
public partial class Form1 : Form { Form2 m_form2 = new Form2(); void Form1_Load(object sender, EventArgs e) {
We dug up a Microsoft source and found that an exception was thrown during a call to DestroyHandle (below). DestroyHandle is called by ShowDialog upon completion.
From source .NET \ 4 \ DEVDIV_TFS \ Dev10 \ Releases \ RTMRel \ ndp \ fx \ src \ WinForms \ Managed \ System \ WinForms \ Control.cs \ 1305376 \ Control.cs:
protected virtual void DestroyHandle() {
Questions:
Why does ShowDialog destroy the handle (when can I reuse this form)?
Why I get an ObjectDisposedException - its very misleading (as it is not used). As if the code was expecting the descriptor to be destroyed only when the object was deleted (this is what I expected).
Should it be valid? That is, should I allow Invoke to the control after ShowDialog?
Notes:
Executing the second .ShowDialog() causes the creation of a new descriptor.
If, after executing .ShowDialog() I try to execute Invoke , I get an InvalidOperationException which states that "Invoke or BeginInvoke cannot be called in the control until the window handle is created."
If, after executing .ShowDialog() I access the Handle property and then execute Invoke , it will succeed.
source share