473,657 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Timers.T imer, 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.T imer(20000);
mTimer.AutoRese t = false;
mTimer.Elapsed += new System.Timers.E lapsedEventHand ler(OnTimerEven t);

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 16878
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.s e> wrote in message
news:uD******** ******@TK2MSFTN GP10.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.T imer(20000);
mTimer.AutoRese t = false;
mTimer.Elapsed += new System.Timers.E lapsedEventHand ler(OnTimerEven t);

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******** ******@TK2MSFTN GP11.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.WriteLi ne 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.T imer tm;

[STAThread]
static void Main(string[] args)
{
tm = new System.Timers.T imer(2000);
tm.Elapsed += new System.Timers.E lapsedEventHand ler(tm_Elapsed) ;

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

private static void tm_Elapsed(obje ct sender,
System.Timers.E lapsedEventArgs e)
{
System.Console. WriteLine("Stop ped");
tm.Enabled = false;
tm.Interval = 300;
}
}

"Peter Johnsson" <lk@algonet.s e> wrote in message
news:um******** ******@TK2MSFTN GP09.phx.gbl...

"Philip Rieck" <st***@mckraken .com> wrote in message
news:uT******** ******@TK2MSFTN GP11.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
2597
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
514
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 working but to get it to work I use a boolean variable (mbLoop) and the program runs until mbLoop is set to False. Do While mbLoop ' Loop
9
7862
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 might need to carry out some back-end processing without pausing the individual users' Session while that process runs. E.g. I might provide the ability for a user to upload a text file of job
10
2698
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 two forms (frmSplash and frmMain) and a code Module setup as my startup object. Here is the code that I have, when I try to run this part of the splash screen form shows and then program terminates. What am I doing wrong? Thanks,
1
2973
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.
1
2112
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: static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); private void dg_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { System.Timers.Timer timer = new System.Timers.Timer();
4
11110
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 the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
5
12213
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
8
3367
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 on a network for files and then copies these files to another location (on the network) AND then updates a record in the database. The file copying is performed before the database update because the file system is not transactional. The code...
0
8324
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,...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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
7353
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
6176
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
4173
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
2743
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
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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.