PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
?!?ParkingWindow - ObjectDisposedException in system.windows.forms
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
?!?ParkingWindow - ObjectDisposedException in system.windows.forms
![]() |
?!?ParkingWindow - ObjectDisposedException in system.windows.forms |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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, |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 -- HTH Stoitcho Goutsev (100) "Sigrun" <Sigrun@discussions.microsoft.com> wrote in message news:65E24A05-DF1A-49D3-98BB-25CF8EFBEB77@microsoft.com... > 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, > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Stoitcho Goutsev (100) wrote:
> 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. |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 "Peter Bromley" wrote: > Stoitcho Goutsev (100) wrote: > > 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. > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
As an MVP, I would like to recommend Microsoft that a KB article be created
explaining this situation. "Sigrun" <Sigrun@discussions.microsoft.com> wrote in message news:098B1687-1C93-4945-B39E-4FF1E7417E58@microsoft.com... > 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 > Sigrun > > "Peter Bromley" wrote: > >> Stoitcho Goutsev (100) wrote: >> > 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. >> |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

