473,810 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer disposed before getting to my destructor?

I have a class object that creates and uses a System.Threadin g.Timer. In
the destructor for my class I cancel the timer using timer.Change(in finite,
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 3811
Hi,
I have a class object that creates and uses a System.Threadin g.Timer. In
the destructor for my class I cancel the timer using timer.Change(in finite,
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****@jimsacc ount.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.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: crlspr-24.233.164.226. myacc.net 24.233.164.226
Path: cpmsftngxa10.ph x.gbl!TK2MSFTNG XA03.phx.gbl!TK 2MSFTNGP08.phx. gbl!TK2MSFTNGP0 9.phx.gbl
Xref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2852 67
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

I have a class object that creates and uses a System.Threadin g.Timer. In
the destructor for my class I cancel the timer using timer.Change(in finite,
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****@jimsacc ount.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
I have a class object that creates and uses a System.Threadin g.Timer. In
the destructor for my class I cancel the timer using timer.Change(in finite,
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.m icrosoft.com> wrote in message
news:Vt******** ******@cpmsftng xa10.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****@jimsacc ount.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.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: crlspr-24.233.164.226. myacc.net 24.233.164.226
Path:
cpmsftngxa10. phx.gbl!TK2MSFT NGXA03.phx.gbl! TK2MSFTNGP08.ph x.gbl!TK2MSFTNG P09.phx.gbl
Xref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2852 67
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

I have a class object that creates and uses a System.Threadin g.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****@jimsacc ount.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Jim H <no****@jimsacc ount.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.co m>
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
15698
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. Thanks Andrew
5
8527
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 System.Threading.Timer.Change(Int32, int32) at Systems.Timers.UpdateTimer() at System.Timers.Timer.set_Interval(double value)
3
5976
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 ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
4
1806
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, can a class instance destroy itself via one of it's own methods?
8
1840
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) { // Set timer to call Page_PostLoad in 10 seconds
2
2863
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 operation, then dispose of the TimerReference. In this case, the operation is simply enabling a checkbox control. I have found that the timer will continue to tick at least twice after it is disposed. Sometimes it will tick several HUNDRED...
1
1413
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 other object. Does any one have any ideas what may be going on here. Thanks
2
3958
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 done. // Instance variables private Thread workerThread = null; private System.Threading.Timer workerThreadMonitorTimer = null; /// Code inside a button click event ThreadStart workerThreadStart = new ThreadStart(DoWork);
5
3278
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 form 2 2. if you have a timer control running on a childform, and you either a. minimize or
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.