473,396 Members | 1,961 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.

System.Timers.Timer, AutoReset doesn't work

How come the eventhandler for the timer's elapsed time event is called over
and over again, even though the AutoReset property is set to false, if you
assign a new value to the timer objects interval property inside the event
handler?

Example follows:

Constructor:
mTimer = new System.Timers.Timer(20000);
mTimer.AutoReset = false;
mTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerEvent);

OnTimerEvent(...):
mTimer.Interval = 10000;

As soon as the timer is started, it will start its countdown and then
finally call the eventhandler, but it will continue doing so forever just
because I'm assigning a new value to its interval property inside the
handler.

// Curious
Nov 15 '05 #1
3 16805
Seting the interval to anything greater than 0 (even what was previously set
at) resets the timer (less than or equal to zero will throw an exception).
It will not re-enable the timer, however.

What you'll probably should do is set the timer's enabled to false in your
event handler, then in the code where you would have reset it, re-enable it.
"Peter Johnsson" <lk@algonet.se> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you
assign a new value to the timer objects interval property inside the event
handler?

Example follows:

Constructor:
mTimer = new System.Timers.Timer(20000);
mTimer.AutoReset = false;
mTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerEvent);

OnTimerEvent(...):
mTimer.Interval = 10000;

As soon as the timer is started, it will start its countdown and then
finally call the eventhandler, but it will continue doing so forever just
because I'm assigning a new value to its interval property inside the
handler.

// Curious

Nov 15 '05 #2

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uT**************@TK2MSFTNGP11.phx.gbl...
Seting the interval to anything greater than 0 (even what was previously set at) resets the timer (less than or equal to zero will throw an exception).
It will not re-enable the timer, however.
Thanks for your answer!

What's the difference between the timer being reset or re-enabled? The timer
do seems to be re-enabled (internally atleast) when a new Interval value is
assigned inside the event handler. However, its Enabled property doesn't
change to True when a value is assigned to Interval, even though it seems to
be enabled interally.
What you'll probably should do is set the timer's enabled to false in your
event handler, then in the code where you would have reset it, re-enable

it.

Doesn't matter. The eventhandler will be called again even though I set the
enabled to false or calling the stop method, like

EventHandler:
mTimer.Interval = 10000;
mTimer.Enabled = false; // or mTimer.Stop().. doesn't matter..

Try it yourself :)
Nov 15 '05 #3
The code below when run will show one timer event fired each time the timer
is enabled.

If you debug it and set a breakpoint in the tm_Elapsed function, however,
please note that timer events may be pending, as the timer can run on
another thread. This is probably the behavior you're seeing. You can get
this same behavior in run mode if you set the interval to something very
small, so that it fires again while the call to Console.WriteLine is
running.

Note the documentation:

"Note The event-handling method might be called even after the Stop method
is called. The event-handling method might run on one thread at the same
time that a call to the Stop method runs on another thread. This might
result in the Elapsed event being raised even after the Stop method is
called. To prevent this, use the SignalTime property to compare the time the
event was raised to the time the Stop method was called. If the event was
raised after the Stop method was called, do not process the event."
------------------------------------------------------------------

public class Class1
{
private static System.Timers.Timer tm;

[STAThread]
static void Main(string[] args)
{
tm = new System.Timers.Timer(2000);
tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);

for(int i=0;true; i++)
{
System.Threading.Thread.Sleep(200);
if( (i % 20 ) == 0)
{
System.Console.WriteLine("Enabled");
tm.Interval = 400;
tm.Enabled = true;
}
}
}

private static void tm_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
System.Console.WriteLine("Stopped");
tm.Enabled = false;
tm.Interval = 300;
}
}

"Peter Johnsson" <lk@algonet.se> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...

"Philip Rieck" <st***@mckraken.com> wrote in message
news:uT**************@TK2MSFTNGP11.phx.gbl...
Seting the interval to anything greater than 0 (even what was previously set
at) resets the timer (less than or equal to zero will throw an exception). It will not re-enable the timer, however.


Thanks for your answer!

What's the difference between the timer being reset or re-enabled? The

timer do seems to be re-enabled (internally atleast) when a new Interval value is assigned inside the event handler. However, its Enabled property doesn't
change to True when a value is assigned to Interval, even though it seems to be enabled interally.
What you'll probably should do is set the timer's enabled to false in your event handler, then in the code where you would have reset it, re-enable it.

Doesn't matter. The eventhandler will be called again even though I set

the enabled to false or calling the stop method, like

EventHandler:
mTimer.Interval = 10000;
mTimer.Enabled = false; // or mTimer.Stop().. doesn't matter..

Try it yourself :)

Nov 15 '05 #4

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

Similar topics

3
by: User | last post by:
Hi, What is the best way to release all resources holded by the Timer (myTimer from class System.Timers.Timer)? Is it: 1- myTimer.dispose 2- myTimer.enabled = false 3- myTimer.close
3
by: RitaG | last post by:
Hi. I Have a VB.Net Windows Application that starts up using Sub Main. It's a program that runs in the background and checks something every minute. I use a Timer to accomplish this and it's...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
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...
1
by: melanieab | last post by:
Hi, I'm have a datagrid, and I'm trying to have a tooltip pop up if a cell has been hovered on for 2 seconds. I was thinking of using DataGrid.Hover, but then decided to try this instead: ...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...
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
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...

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.