By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Cannot access disposed object

P: n/a
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

Jun 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Jun 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.