473,416 Members | 1,868 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,416 software developers and data experts.

Cannot Access a disposed Object - System.Timer

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
5 8466
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
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

"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
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


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

Similar topics

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()
0
by: Asaf | last post by:
Hi, When I am doing a POST to a SSL URL I am getting this error on first attempt "Cannot access a disposed object named "System.Net.TlsStream"." After the first attempt all works fine, here is the...
3
by: Shawn | last post by:
Hi. In my application I store information about the user in the session object. Since I'm storing sensitive information I encrypt it before storing and decrypt it when I need to use it again. The...
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: Bernie Yaeger | last post by:
I'm launching one window from another. I have a close button in the first window and when I click it, it executes me.close. However, the form also has a treeview control. If I launch the second...
7
by: ed | last post by:
Hi, Here's my code 'in form WithEvents formReplace As New Form Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Clic formReplace.Show(
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...
0
by: sdanda | last post by:
Hai i am working on vb.net. In my application I created four forms.Those are first.vb,f1.vb,f2.vb and f3.vb In firstvb I added 3 checkboxes and a "display" button.The 3 checkboxes are used to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.