R
Ryan Park
Hi,
//SITUATION
I got a panel control that hold a certain position on a form.
Every controls or UIs are on this panel.
At certain situation, I called dispose() method of this panel control
and change it with other panel which contains other business logic and
UI controls.
//ACTUAL CODE PRAGMENT & SYMPTOMS
//_body is a member panel control on the form control.
//And on this panel there is another panel control that holds other UI
controls.
if(_body.Controls.Count > 0)
{
_body.Controls[0].Dispose(); //always ther is only 1 child.
_body.Controls.Clear();
}
_body.Controls.Add(control);
Application.DoEvents();
This works well. But after some time ( a few seconds maybe), I got
"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".
LoginForm is a control type. It's not a name of control.
And this LoginForm is on the _body.Controls[0] panel.
And LoginForm.Dispose() get called when _body.Controls[0].Dispose();
line executes.
//MY QUESTION
I have no idea how this happen. Since I call dispose(), I think there
should be no reference to panel instance.
I've tried GC.SuppressFinalize(), but not works either.
How can I fix this code? Or Is this a bug of .NET?
Any suggestion would be appreciated. TIA.
Regards,
Ryan
//POST SCRIPT - WHOLE ERROR MESSAGE
"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".\r\nObject name: \"LoginForm\".\r\n at
System.Windows.Forms.Control.CreateHandle()\r\n at
System.Windows.Forms.Control.get_Handle()\r\n at
System.Windows.Forms.ContainerControl.FocusActiveControlInternal()\r\n
at System.Windows.Forms.Form.set_Active(Boolean value)\r\n at
System.Windows.Forms.Form.WmActivate(Message& m)\r\n at
System.Windows.Forms.Form.WndProc(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg,
HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)\r\n at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.Application.Run(Form mainForm)\r\n at
WindowsApplication1.Form1.Main() in
c:\\src\\smart\\windowsapplication1\\form1.cs:line 88"
//SITUATION
I got a panel control that hold a certain position on a form.
Every controls or UIs are on this panel.
At certain situation, I called dispose() method of this panel control
and change it with other panel which contains other business logic and
UI controls.
//ACTUAL CODE PRAGMENT & SYMPTOMS
//_body is a member panel control on the form control.
//And on this panel there is another panel control that holds other UI
controls.
if(_body.Controls.Count > 0)
{
_body.Controls[0].Dispose(); //always ther is only 1 child.
_body.Controls.Clear();
}
_body.Controls.Add(control);
Application.DoEvents();
This works well. But after some time ( a few seconds maybe), I got
"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".
LoginForm is a control type. It's not a name of control.
And this LoginForm is on the _body.Controls[0] panel.
And LoginForm.Dispose() get called when _body.Controls[0].Dispose();
line executes.
//MY QUESTION
I have no idea how this happen. Since I call dispose(), I think there
should be no reference to panel instance.
I've tried GC.SuppressFinalize(), but not works either.
How can I fix this code? Or Is this a bug of .NET?
Any suggestion would be appreciated. TIA.
Regards,
Ryan
//POST SCRIPT - WHOLE ERROR MESSAGE
"System.ObjectDisposedException: Cannot access a disposed object named
\"LoginForm\".\r\nObject name: \"LoginForm\".\r\n at
System.Windows.Forms.Control.CreateHandle()\r\n at
System.Windows.Forms.Control.get_Handle()\r\n at
System.Windows.Forms.ContainerControl.FocusActiveControlInternal()\r\n
at System.Windows.Forms.Form.set_Active(Boolean value)\r\n at
System.Windows.Forms.Form.WmActivate(Message& m)\r\n at
System.Windows.Forms.Form.WndProc(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg,
HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)\r\n at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.Application.Run(Form mainForm)\r\n at
WindowsApplication1.Form1.Main() in
c:\\src\\smart\\windowsapplication1\\form1.cs:line 88"