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

Timer disposed before getting to my destructor?

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim
Nov 16 '05 #1
6 3788
Hi,
I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception


Your class should implement IDisposable:

http://msdn.microsoft.com/library/de...izedispose.asp

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
timer.Dispose()
}
}

bye
Rob
Nov 16 '05 #2
Hi Jim

Don't access other objects in a finalizer (destructor). When your object's finalizer is run, the Timer object may have already been finalized. I would recommend your object
implement the IDisposable interdace, and call timer.Dispose in your object's Dispose.

-Chris
--------------------
From: "Jim H" <no****@jimsaccount.com>
Subject: Timer disposed before getting to my destructor?
Date: Mon, 8 Nov 2004 13:20:16 -0500
Lines: 13
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <#A**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: crlspr-24.233.164.226.myacc.net 24.233.164.226
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFT NGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:285267
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Nov 16 '05 #3
Thanks guys
"Jim H" <no****@jimsaccount.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim

Nov 16 '05 #4
One question, what's a destructor supposed to be used for if I shouldn't
access member objects? I thought the purpose of a destructor was to do
cleanup for the current object.

jim
""Chris Lyon [MSFT]"" <cl***@online.microsoft.com> wrote in message
news:Vt**************@cpmsftngxa10.phx.gbl...
Hi Jim

Don't access other objects in a finalizer (destructor). When your
object's finalizer is run, the Timer object may have already been
finalized. I would recommend your object
implement the IDisposable interdace, and call timer.Dispose in your
object's Dispose.

-Chris
--------------------
From: "Jim H" <no****@jimsaccount.com>
Subject: Timer disposed before getting to my destructor?
Date: Mon, 8 Nov 2004 13:20:16 -0500
Lines: 13
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <#A**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: crlspr-24.233.164.226.myacc.net 24.233.164.226
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MS FTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:285267
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using
timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This
is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim

--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 16 '05 #5
Jim H <no****@jimsaccount.com> wrote:
One question, what's a destructor supposed to be used for if I
shouldn't access member objects? I thought the purpose of a
destructor was to do cleanup for the current object.


Only cleanup which the contained objects can't do for themselves.
Usually, you don't need a destructor unless you *directly* contain
unmanaged resources. The destructors for streams etc can take care of
things themselves - but implement IDisposable whether you contain the
unmanaged resources directly or indirectly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Thanks

jim

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jim H <no****@jimsaccount.com> wrote:
One question, what's a destructor supposed to be used for if I
shouldn't access member objects? I thought the purpose of a
destructor was to do cleanup for the current object.


Only cleanup which the contained objects can't do for themselves.
Usually, you don't need a destructor unless you *directly* contain
unmanaged resources. The destructors for streams etc can take care of
things themselves - but implement IDisposable whether you contain the
unmanaged resources directly or indirectly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
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...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
4
by: Peter Oliphant | last post by:
I'd like to be able to destroy a Timer in it's own event handler. That is, within it's tick handler I'd like to delete the Timer itself (e.g., for one-shot timers). Is this possible? In general,...
8
by: Jim Hammond | last post by:
The following code tries to excute a function 10 seconds after Page_Load by using a Timer, but the callback never gets called. private void Page_Load(object sender, System.EventArgs e) { ...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
1
by: Adam | last post by:
I have code that has a timer object and after running for a while it throws an error "Cannot access a disposed object named "Timer". Object name: "Timer"." I never dispose this object or any...
2
by: Dipankar | last post by:
In an Windows application I have created a Timer to monitor some WorkerThread's state. Once the worker thread stops the timer should also dispose after displaying the message that Worker Thread is...
5
by: Peted | last post by:
Just some threading questions 1. in a MDI app if you have multiple child forms, does each child form run in its own thread ? Eg do you get error trying to update one control on form1 from...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.