Cannot Access a disposed Object - System.Timer

  • Thread starter Thread starter theinvisibleGhost
  • Start date Start date
T

theinvisibleGhost

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
 
theinvisibleGhost said:
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
 
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.....
 
theinvisibleGhost said:
Any more ideas anyone?
Cheers


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.
 
Back
Top