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

Cannot Access a disposed Object - System.Timer

P: n/a
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
System.Threading.Timer.Change(Int32, int32)
at Systems.Timers.UpdateTimer()
at System.Timers.Timer.set_Interval(double value)

and so on thru my app.......

The problem occurs when i set the interval on the timer.
Sometimes this occurs within 4 minutes of starting the app....
The record today is 22 hours........

Theres nothing in my code to dispose of the timers
they should just sit there....
any ideas?! :-s

Cheers

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
theinvisibleGhost wrote:
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
System.Threading.Timer.Change(Int32, int32)
at Systems.Timers.UpdateTimer()
at System.Timers.Timer.set_Interval(double value)

and so on thru my app.......

The problem occurs when i set the interval on the timer.
Sometimes this occurs within 4 minutes of starting the app....
The record today is 22 hours........

Theres nothing in my code to dispose of the timers
they should just sit there....
any ideas?! :-s

Cheers


Apart from calling Dispose "by hand" (=hardcoded), an object could (will)
get disposed if all references to it go out of scope. Could that be the case
here?

You might show some more details of your app: where do you create
that timer? Where do you use it? Is it a win-form or web-form app?
etc.
Hans Kesting
Nov 17 '05 #2

P: n/a
Thanks for your reply.
The app is windows based.
I'm having this occur in more than once class, although it's occured
most in a class
I call LEDControl (Simulates an LED!). The LED has a flash timer (to
make it flash!)
The timer is created as a private attribute on creation of the LED
object.
No dispose methods are called within the class except on disposal of
the LED object.
The LED itself is definitley not disposed of (I can see it for a
start)....
The timer is never set to null or infact set to anything after the
line

private System.Timers.Timer flashTimer = new System.Timers.Timer();

so as far as I can see it should always have one and only onereference
to it, until
the LED is disposed off (When the application ends).

I tried placing a MessageBox in the Dispose event of the LED, and it
never showed,
so the timer cannot have been disposed of there.....

Nov 17 '05 #3

P: n/a

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
an object could (will) get disposed if all references to it go out of
scope.


This is not true.

- Michael S
Nov 17 '05 #4

P: n/a
Any more ideas anyone?
Cheers

theinvisibleGhost wrote:
Thanks for your reply.
The app is windows based.
I'm having this occur in more than once class, although it's occured
most in a class
I call LEDControl (Simulates an LED!). The LED has a flash timer (to
make it flash!)
The timer is created as a private attribute on creation of the LED
object.
No dispose methods are called within the class except on disposal of
the LED object.
The LED itself is definitley not disposed of (I can see it for a
start)....
The timer is never set to null or infact set to anything after the
line

private System.Timers.Timer flashTimer = new System.Timers.Timer();

so as far as I can see it should always have one and only onereference
to it, until
the LED is disposed off (When the application ends).

I tried placing a MessageBox in the Dispose event of the LED, and it
never showed,
so the timer cannot have been disposed of there.....


Nov 17 '05 #5

P: n/a


theinvisibleGhost wrote:
Any more ideas anyone?
Cheers

theinvisibleGhost wrote:
Thanks for your reply.
The app is windows based.
I'm having this occur in more than once class, although it's occured
most in a class
I call LEDControl (Simulates an LED!). The LED has a flash timer (to
make it flash!)
The timer is created as a private attribute on creation of the LED
object.
No dispose methods are called within the class except on disposal of
the LED object.
The LED itself is definitley not disposed of (I can see it for a
start)....
The timer is never set to null or infact set to anything after the
line

private System.Timers.Timer flashTimer = new System.Timers.Timer();

so as far as I can see it should always have one and only onereference
to it, until
the LED is disposed off (When the application ends).

I tried placing a MessageBox in the Dispose event of the LED, and it
never showed,
so the timer cannot have been disposed of there.....

Found the problem, eventually!
It is a known bug if you search hard enough across the Net.
I found a link
http://www.kbalertz.com/kb_842793.aspx
Basically as soon as you stop a timer, the internal
System.Threading.Timer becomes available for Garbage Collection,
sometimes causing the elapsed event not to occur, or sometimes
causing a disposed reference exception.

Although not described in the article, my soloution was
to create a new timer every time the timer was to be
stopped and re-add the elapsed events. Not efficient but easy,
and not a problem processor wise with me.
This has totally solved my problem.
Cheers for all who responded.
T.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.