tags:

views:

694

answers:

3

Hi everyone,

I'm having 2 forms. From one form I created and shown the other form. It's working great. But when I try to close or Dispose that form from the form that created it I get following Exception:

Exception :
   Value Dispose() cannot be called while doing CreateHandle().

Stack Trace :
========================
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.Label.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
   at System.Windows.Forms.Form.Dispose(Boolean disposing)
   at Speedometer_Application.frmSpeedometer.Dispose(Boolean disposing) 

Any idea????

A: 

The code is as follows:

if (frmMain.objfrm== null)
{   
     frmMain.objfrm = frmMyForm.Instance;     
     frmMain.objfrm.ShowInTaskbar = false;    
}
frmMain.objfrm.Show();
frmMain.objfrm.BringToFront();

frmMain is the Main Form that has a static variable of the frmMyForm. than in my code whereever i want to use this I just check if it's not null than create it using a static Instance and than give the peoperty.

While closing the form I have the following code:

frmMain.objfrm.Close_this();

The Close_this calls the Close() or Dispose() method.

But when I call that function I get the above exception.

Jankhana
+1  A: 

Hi, it is hard to say what is the problem from the code you posted.

The code that you posted should work (form shown with Show() should be possible to close with Dispose() method).

The reason why it does not work is probably somewhere in the form that you are trying to dispose of. When you call Dispose() method (according to the error message this is what happens) on an object, that object will try to dispose of all its children and do some cleanup. That is the place to look for error.

My suggestion is to comment out all your code in the form objfrm (or make new EMPTY form) and see if error happens. It should not happen. Then start adding commented code and see when the error happens. I bet it will be in the code that is being called as consequence of Dispose method.

Ya that's the problem I have a timer that's ticking and a delegate event is called every 100 milliseconds. so when it's try to dispose the form that event is running in the worker's thread and I get the exception!!! But i need that event to be triggered every 100 milliseconds. so now what could be the solution???
Jankhana
A: 

You need to use ShowDialog instead of Show thats the problem. Show dont block the application and the code keeps running.

You are disposing the object when the GUI is creating it (that what the exception said)

Try with this:

if (frmMain.objfrm== null)
{   
     frmMain.objfrm = frmMyForm.Instance;     
     frmMain.objfrm.ShowInTaskbar = false;    
}
frmMain.objfrm.ShowDialog();

Note the ShowDialog()

MarcosMeli
ShowDialog() is not working. still the same exception is coming!!!
Jankhana
And why u use .Instance ? can u try doing: frmMain.objfrm = new frmMyForm();Cheers
MarcosMeli