?
Solved

How to reset a system.timers.Timer

Posted on 2009-02-11
6
Medium Priority
?
1,913 Views
Last Modified: 2012-05-06
Hi

I'm trying to reset a system.timer.Timer.
I have a textbox which checks for user input with the TextChanged event.
What I'm trying to do is, if the text in the textbox hasnt changes for 2 sec, the timer.elapsed event should fire, if the text changes before 2 sec, the timer should reset and start over. See code below

As I understand timer.Autoreset only sets if the timer should run more than one time?
Then how do I reset the timer if the text changes before 2 sec?

Regards
Marius
System.Timers.Timer tmrTextChanged = new System.Timers.Timer(2000);
 
//My textchanged event
private void tbSearchField_TextChanged(object sender, EventArgs e)
{
         tmrTextChanged.Enabled = true;
         tmrTextChanged.AutoReset = true;
         tmrTextChanged.Elapsed += new System.Timers.ElapsedEventHandler(tmrTextChanged_Tick); 
}
//My timer elapsed event
private void tmrTextChanged_Tick(object sender, EventArgs e)
{
       Messagebox.Show("Waited for 2 sec");
       tmrTextChanged.Enabled = false;
}

Open in new window

0
Comment
Question by:Contestoas
6 Comments
 
LVL 3

Expert Comment

by:Blomholm
ID: 23609762
Well this should work. Seems a bit dirty to create a new timer object each time, but don't know of any other solution.
System.Timers.Timer tmrTextChanged = new System.Timers.Timer(2000);
 
//My textchanged event
private void tbSearchField_TextChanged(object sender, EventArgs e)
{
         tmrTextChanged = new System.Timers.Timer(2000);
         tmrTextChanged.Enabled = true;
         tmrTextChanged.Elapsed += new System.Timers.ElapsedEventHandler(tmrTextChanged_Tick); 
}
//My timer elapsed event
private void tmrTextChanged_Tick(object sender, EventArgs e)
{
       Messagebox.Show("Waited for 2 sec");
}

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23609765
Disable and enable the timer again in the tbSearchField_TextChanged event and also please dont subscript to the Elapsed event in this function as it will keep adding the subscribers.
tmrTextChanged.Enabled = false;
tmrTextChanged.Enabled = true;

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23609774
Reset timer:
thisTimer.Enabled = False
thisTimer.Enabled = True


private void tbSearchField_TextChanged(object sender, EventArgs e)
{
         tmrTextChanged.Enabled = false;
         tmrTextChanged.Enabled = true;
         tmrTextChanged.AutoReset = true;
         tmrTextChanged.Elapsed += new System.Timers.ElapsedEventHandler(tmrTextChanged_Tick); 
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Expert Comment

by:elimesika
ID: 23609775
HI

If Interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set Enabled to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.



Source
http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 23609777
Like this
System.Timers.Timer tmrTextChanged = new System.Timers.Timer(2000);
tmrTextChanged.Elapsed += new System.Timers.ElapsedEventHandler(tmrTextChanged_Tick); 
//My textchanged event
private void tbSearchField_TextChanged(object sender, EventArgs e)
{
   tmrTextChanged.Enabled = false;
   tmrTextChanged.Enabled = true;
}
//My timer elapsed event
private void tmrTextChanged_Tick(object sender, EventArgs e)
{
       Messagebox.Show("Waited for 2 sec");
}

Open in new window

0
 

Author Comment

by:Contestoas
ID: 23609853
Thanks for all the reply, got it working with Sunithnair's solution

Runs like a charm now:)

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question