473,396 Members | 2,158 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,396 software developers and data experts.

Destroy a Timer object in its callback method

Hi,

I have a timer object that's launched as below:

mTimer = new System.Threading.Timer (new
TimerCallBack(SubscribeTrade), null, 15000, 15000);

void SubscribeTrade (object state)
{
DateTime now = DateTime.Now;
TimeSpan elapsed = now.Subtract(mStartTime);
int minutes = elapsed.Minutes;

if (minutes <= 30)
{
//Code omitted here
}
else // Need to destroy the timer after 30 minutes here
{
// Is this the right way to destroy the timer object?
mTimer.Dispose();
}
}

As shown in my code, I need to destroy the timer after it's run for 30
minutes. Will it cause a problem if I dispose the timer inside its
callback method, "SubscribeTrade"?

If it will, how can I destroy it in 30 minutes?
Jun 27 '08 #1
4 2922
Curious,

I don't like system.threading.timers as it is working even as the thread has
stopped.

However, what do you mean with destroy, disable it?

Be aware that dispose is not a kind of command to finalize things. Dispose
is a method inherited from component which is in almost 20% of every class
(as Jon once wrote) which you can use to overload to release non manages
resources.

Cor

"Curious" <fi********@yahoo.comschreef in bericht
news:6e**********************************@l42g2000 hsc.googlegroups.com...
Hi,

I have a timer object that's launched as below:

mTimer = new System.Threading.Timer (new
TimerCallBack(SubscribeTrade), null, 15000, 15000);

void SubscribeTrade (object state)
{
DateTime now = DateTime.Now;
TimeSpan elapsed = now.Subtract(mStartTime);
int minutes = elapsed.Minutes;

if (minutes <= 30)
{
//Code omitted here
}
else // Need to destroy the timer after 30 minutes here
{
// Is this the right way to destroy the timer object?
mTimer.Dispose();
}
}

As shown in my code, I need to destroy the timer after it's run for 30
minutes. Will it cause a problem if I dispose the timer inside its
callback method, "SubscribeTrade"?

If it will, how can I destroy it in 30 minutes?
Jun 27 '08 #2
On Jun 10, 12:50*am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
Curious,

I don't like system.threading.timers as it is working even as the thread has
stopped.

However, what do you mean with destroy, disable it?
I mean to disable it. Having a timer run periodically is a waste of
resources even when it does nothing. I actually want to free the
memory occupied by the timer. However, I care less about the memory
than stopping the timer from running.

Any advice?
Jun 27 '08 #3
On Tue, 10 Jun 2008 05:44:25 -0700 (PDT), Curious
<fi********@yahoo.comwrote:
>On Jun 10, 12:50*am, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
>Curious,

I don't like system.threading.timers as it is working even as the thread has
stopped.

However, what do you mean with destroy, disable it?

I mean to disable it. Having a timer run periodically is a waste of
resources even when it does nothing. I actually want to free the
memory occupied by the timer. However, I care less about the memory
than stopping the timer from running.

Any advice?
You can stop it using the Change() method:
<http://msdn.microsoft.com/en-us/library/yz1c7148.aspx>.

This code <http://msdn.microsoft.com/en-us/library/ms149618.aspx>
shows calling Dispose() from the callback method, so unless the
documentation lies it is OK to do so.
Jun 27 '08 #4
Thanks Jack! I'll give it a try to call

mTimer.Dispose();

from the callback method to destroy the mTimer entirely.
Jun 27 '08 #5

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

Similar topics

8
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the...
4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
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) { ...
4
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
3
by: anthony | last post by:
Hi, there is sth i am not sure, i looked at the help in vb.net for creating timer on the fly (see below). Since the event would happen 10sec later, I cannot destroy the timer right away (or can...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
1
by: logdenav | last post by:
Hello I'm testing the performance of the System.Timers.Timer class. I created a small program that create 100 User objects. Each USer object create a MyTimer object. The constructor of the User...
4
by: jwriteclub | last post by:
Hello all, I have a quick question about timers in C#. I have a System.Windows.Forms.Timer timer (the kind you "drag" out of the toolbox. It looks something like this: public class...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
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?
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
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...
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,...

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.