?!?ParkingWindow - ObjectDisposedException in system.windows.forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.
Im programming a Windows Forms application that has many user controls (e.g.
charts). These user controls have System.Threading.TimerCallback functions.
These functions Invoke other member methods that update the user controls
e.g.:

private void OnTimerUpdate(object pState)
{
if( mMainChart.InvokeRequired )
{
Invoke(new UpdateAndCleanUpChartDelegate(UpdateAndCleanUpChart));
}
else
{
UpdateAndCleanUpChart();
}
}

After calling Application.Exit() I get a bunch of message boxes with the
following messages:

System.ObjectDisposedException: Cannot access a disposed object named
"ParkingWindow".
Object name: "ParkingWindow".
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate
method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at MyChartClass.OnTimerUpdate(Object pState)An unhandled exception of
type 'System.ObjectDisposedException' occurred in system.windows.forms.dll

Additional information: Cannot access a disposed object named "ParkingWindow".

This happens randomly, i.e. for different user controls each time.

Could anyone help me out here, how do I prevent this or handle the problem.
Regards,
 
Sigrun,

You need to stop those timers before closing the application. It looks like
they fire events that you try to marshal to the UI thread while the controls
are being disposed.

Keep in mind that depending on the timer you may receive tick after shuting
down the timer. You need to handle these situations and not to try
marshaling the call. You can use for example Control's Disposing and
Disposed properties before calling InvokeRequired and Invoke
 
Stoitcho said:
Sigrun,

You need to stop those timers before closing the application. It looks like
they fire events that you try to marshal to the UI thread while the controls
are being disposed.

Keep in mind that depending on the timer you may receive tick after shuting
down the timer. You need to handle these situations and not to try
marshaling the call. You can use for example Control's Disposing and
Disposed properties before calling InvokeRequired and Invoke
Actually, its worse than that, because the object may not be disposed
or disposing when you call Invoke and be disposed by the time the
message pump attempts to dispatch the Invoke (and generates the exception)

What I do is try/catch the Invoke call and swallow any
ObjectDisposedException that is generated.

C++ code:

try
{
Invoke(...)
}
catch (System::ObjectDisposedException* ex)
{}


HTH.
 
Thenk you Peter and Stoitcho.
This was useful to me, I have added a check of the disposing property, if
the object is not disposing, I go through the Invoke phase using try-catch.
This handles the problem. It would be nice if someone from Microsoft would
point out how to AVOID the problem though.
rgds
Sigrún
 
As an MVP, I would like to recommend Microsoft that a KB article be created
explaining this situation.
 
Back
Top