Click here to Skip to main content
15,886,104 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

Can anybody tell me why i get "Cannot access a disposed object" error when i am migrating from 1 form to another without closing the previous form. To be precise i am using windows form as modal forms, and i have started the timer when i am in the 1st form & when i switch to 2nd form i am stopping the timer. Also i have used delegates to process data from a library & this library is accessed from both the forms. So i get this error in the delegate event in the 2nd form when trying to open 2nd form. So how to handle this type of error? The error what i get is "Cannot access a disposed object." "Object name: 'Form2'".

So can anybody tell me how to safely switch between forms without affecting any of the stuffs like those...?
Posted
Comments
Rohit Kumar Mumbai 22-Mar-13 2:53am    
Can you please paste exact code where you are getting the exception?
Jagadisha_Ingenious 22-Mar-13 3:20am    
I have added the piece of code here..
_Maxxx_ 22-Mar-13 2:55am    
I don't understand how you are swapping between modal forms - only one form can be modal at a time...

I suggest you post some code so we can see where you are going wrong.
Jagadisha_Ingenious 22-Mar-13 3:21am    
I have added the piece of code here..
Jagadisha_Ingenious 22-Mar-13 3:03am    
#region Delegate Functions
public void DoGUIClear()
{
if (this.InvokeRequired)
{
GUIClear delegateMethod = new GUIClear(this.DoGUIClear);
this.Invoke(delegateMethod);
}

// this.lstRegisterValues.Items.Clear();
}
public void DoGUIStatus(string paramString)
{
if (this.InvokeRequired)
{
GUIStatus delegateMethod = new GUIStatus(this.DoGUIStatus);
this.Invoke(delegateMethod, new object[] { paramString });
//this.lblStatus.Text = paramString;
}
/* else
this.lblStatus.Text = paramString;*/
}
public void DoGUIUpdate(string paramString)
{
if (this.InvokeRequired)
{
GUIDelegate delegateMethod = new GUIDelegate(this.DoGUIUpdate);
this.Invoke(delegateMethod, new object[] { paramString });
}
/* else
this.lstRegisterValues.Items.Add(paramString);*/
}
#endregion
This is the delegate function which i am using.

The following to switch.
private void btnOk_Click(object sender, EventArgs e)
{
if (form2 == null || form2.IsDisposed)
{
form2 = new Annunciator_Window(descriptionName, btnName.Text, Port, devicescount, SlvID);
form2.MdiParent = MdiParent;
form2.WindowState = FormWindowState.Maximized;
// myPictureForm.FormClosing += form2_Closing;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
}
this.Hide();
form2.Show();
}

1 solution

From the comments it is clear that the Form is Disposed.
Add a breakpoint to the
C#
protected override void Dispose(bool disposing)
method of your form and check the call stack who caused the disposal of the Form.
 
Share this answer
 
Comments
toATwork 22-Mar-13 5:31am    
Is the issue resolved now? Which part exactly did the disposal?
Jagadisha_Ingenious 22-Mar-13 5:34am    
I have put the breakpoint as u said but when i run it doesn't stop @ the breakpoint...
toATwork 22-Mar-13 5:38am    
try to dump the StackTrace:
System.Console.WriteLine(Environment.StackTrace);
Jagadisha_Ingenious 22-Mar-13 6:03am    
After stack trace i got this data in HTMl view.. by the way i have put that line of code in the button click event while migrating from 1 form to another.

" at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at AnnunciatorMonitoringTool.Annunciator_Devices.Annunciator_Devices_Click(Object sender, EventArgs e) in C:\AnnunciatorMonitoringTool\AnnunciatorMonitoringTool\Annunciator Devices.cs:line 462
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at AnnunciatorMonitoringTool.Program.Main() in C:\AnnunciatorMonitoringTool\AnnunciatorMonitoringTool\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()".
toATwork 22-Mar-13 6:06am    
This function is causing your Dispose:
Annunciator_Devices_Click(Object sender, EventArgs e)

Please provide the code of it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900