Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cannot access disposed object

1 view
Skip to first unread message

Amit Dedhia

unread,
Jun 8, 2007, 5:56:35 AM6/8/07
to
Hi

I am having problem working with Timers in C++/CLI (the .NET version
of C++)

I have an application which has several forms with pictureBox controls
on it. There is a background timer executing every 250ms and it
generates a display image (using some real time data) and pastes those
on the picture box. As the picturebox was generated by main
application thread, I used 'control.Invoke()' in the timer function
for displaying the image.

The problem starts when I close the form. In Form_CLose event I
dispose the timer. And then the form also gets disposed. However, even
after this the timer function is executed at least once (it is
possible that the timer was executing when timer->dispose() was
called). This function eventually calls the control.Invoke and then I
get ObjectDisposed exception saying that 'Disposed object cannot be
accessed'. I tried using both System.Threading.Timer and
System.Timers.Timer.

Is there any solution/workaround for this?

I tried control.BeginInvoke() instead of control.Invoke().
BeginInvoke() returns immidately and the method executes
asynchroneously in a threadpool therad. This appearently solves the
issue. However I am not too sure about it.

I have, however, few doubts related to this. As the threads are
running very short intervals (250ms) is it possible that there are
more than one thread pool threads pending and the second one executes
before the first one?

Also - lets say one control.Invoke() is waiting execution in thread
pool thread. The form closes before the thread pool function executes
(the application is still alive as there are other forms). What
happens to thread pool thread then? Does it execute? If yes, what
about the exceptions (it will surely generate exception as the form is
disposed). If it does not execute, then does that threadpool thread
get recycled and be avaialble to others?
(I see that the thread count keeps increasing in my application. On
the end user machine it increases to very high value)

Please can some give me a solution and solve my queries?

Thanks in advance
Best regards
Amit Dedhia

Joe Seigh

unread,
Jun 8, 2007, 7:09:19 AM6/8/07
to
Amit Dedhia wrote:
> Hi
>
> I am having problem working with Timers in C++/CLI (the .NET version
> of C++)
>
> I have an application which has several forms with pictureBox controls
> on it. There is a background timer executing every 250ms and it
> generates a display image (using some real time data) and pastes those
> on the picture box. As the picturebox was generated by main
> application thread, I used 'control.Invoke()' in the timer function
> for displaying the image.
>
> The problem starts when I close the form. In Form_CLose event I
> dispose the timer. And then the form also gets disposed. However, even
> after this the timer function is executed at least once (it is
> possible that the timer was executing when timer->dispose() was
> called). This function eventually calls the control.Invoke and then I
> get ObjectDisposed exception saying that 'Disposed object cannot be
> accessed'. I tried using both System.Threading.Timer and
> System.Timers.Timer.
>
> Is there any solution/workaround for this?
[...]

>
> Please can some give me a solution and solve my queries?
>

This is the determinististic object deletion in a GC environment
problem.

You need to use reference counting or something to delay
disposing the form until no other threads are referencing
it. Or use state info and test the state before using
it, in a locked region of course.


--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.

0 new messages