473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destroy a Timer object in its callback method

Hi,

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

mTimer = new System.Threadin g.Timer (new
TimerCallBack(S ubscribeTrade), null, 15000, 15000);

void SubscribeTrade (object state)
{
DateTime now = DateTime.Now;
TimeSpan elapsed = now.Subtract(mS tartTime);
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 2936
Curious,

I don't like system.threadin g.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********@yah oo.comschreef in bericht
news:6e******** *************** ***********@l42 g2000hsc.google groups.com...
Hi,

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

mTimer = new System.Threadin g.Timer (new
TimerCallBack(S ubscribeTrade), null, 15000, 15000);

void SubscribeTrade (object state)
{
DateTime now = DateTime.Now;
TimeSpan elapsed = now.Subtract(mS tartTime);
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.threadin g.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********@yah oo.comwrote:
>On Jun 10, 12:50*am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
wrote:
>Curious,

I don't like system.threadin g.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
2165
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 "Go" eats more and more RAM, is it normal? Can it be avoided? (In normal programs this is isn't a real problem). ! import Tkinter
4
4048
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 like this: TimerCallback callBack = new TimerCallback(Run); // run is obviously a method
8
1831
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
4
2659
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 Application_Start method. It seems to call the callback once or twice while the application starts up but appears to stop once the default page is displayed. Here is the code below (from Global.asax.cs): public static void ProcessAccounts (object state)...
3
1505
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 I? or will VB.net destroy automatically?), I can do it after the event is fired, but the timer is not in scope in the event subroutine. If I leave it as is, if the application continue to fire the timer over time, then it would continue to consume...
8
2718
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 thread to access the database and then displays a modal dialog which allows the user to cancel the task, if it is taking longer than they want, and shows them a display of how long the query has been running so far.
1
2972
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 class contains a name and a time to wait for the timer. Calling start on the User objet initiate the MyTimer to callback in 1 second.
4
1829
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 Class {
4
5712
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 everyting is secured by Invoke/BeginInvoke, and synchronisation primitves like WaitHandles, Evetns, Semaphores, etc... All works good and with no race conditions or locks. I have a System.Windows.Forms.Timer in the main
0
8063
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
8498
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8341
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
6817
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...
1
6014
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5476
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
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
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.