473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Cannot access disposed object

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
1 2208
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: salih ataoz | last post by:
i close a form and i want to open it again it show me Cannot access a disposed object at vb6 this is ok at comman1_click form1.show
7
by: Ryan Park | last post by:
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...
5
by: D Witherspoon | last post by:
I use the following code to open up a form. ------------------------------------------------------- If fImage Is Nothing Then fImage = New frmImage End If fImage.Show()
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
5
by: mthgk | last post by:
I have a C# MDI app. The child forms do alot of work, so this work is perfomed on a different thread created using ThreadPool.QueueUserWorkItem(). Because the status of the work is important to...
3
by: Tracey | last post by:
sorry I post this problem again. I have to stop my work to fix the problem. I'm doing a multi form application(Not a MDI one). My startup form say Form1 has a datagrid say datagrid1, when I...
2
by: Rajat Tandon | last post by:
Hi, I have a grid which is continuously updating by the data from a external event. When I close the form on which the grid is placed, then it gives the error message ... "Can not access a...
1
by: Amit Dedhia | last post by:
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...
4
JustRun
by: JustRun | last post by:
Hi All, I'm developing a windows desktop solution using VC# , I deal with my database using Dataset. My Problem that i'm trying to call a Form to display a confirmation message after every...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.