Friday, February 1, 2013

C# Execute Method every X Seconds Until a condition is met and then stop

C# Execute Method every X Seconds Until a condition is met and then stop

I'm using a Silverlight C# Button click event to pause 10 seconds after click then call a method every x seconds until a specific condition is met: Either x=y or elapsed seconds>=60 without freeziing the UI.

There are several different examples out there. I am new to C# and am trying to keep it simple. I came up with the following, but I don't have the initial 10 second wait which I need to understand where to put and it seems like I have an endless loop. Here is my code:

  public void StartTimer()     {          System.Windows.Threading.DispatcherTimer myDispatchTimer = new System.Windows.Threading.DispatcherTimer();          myDispatchTimer.Interval = TimeSpan.FromSeconds(10); // initial 10 second wait         myDispatchTimer.Tick += new EventHandler(Initial_Wait);         myDispatchTimer.Start();     }      void Initial_Wait(object o, EventArgs sender)     {         System.Windows.Threading.DispatcherTimer myDispatchTimer = new System.Windows.Threading.DispatcherTimer();         // Stop the timer, replace the tick handler, and restart with new interval.          myDispatchTimer.Stop();         myDispatchTimer.Tick -= new EventHandler(Initial_Wait);         myDispatchTimer.Interval = TimeSpan.FromSeconds(5); //every x seconds         myDispatchTimer.Tick += new EventHandler(Each_Tick);         myDispatchTimer.Start();     }       // Counter:     int i = 0;      // Ticker     void Each_Tick(object o, EventArgs sender)     {               GetMessageDeliveryStatus(messageID, messageKey);             textBlock1.Text = "Seconds: " + i++.ToString();       } 

Answers & Comments...

Answer: 1

Create a second event handler that changes the timer. Like this:

public void StartTimer() {     System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();      myDispatchTimer.Interval = TimeSpan.FromSeconds(10); // initial 10 second wait     myDispatchTimer.Tick += new EventHandler(Initial_Wait);     myDispatchTimer.Start(); }  void Initial_Wait(object o, EventArgs sender) {     // Stop the timer, replace the tick handler, and restart with new interval.     myDispatchTimer.Stop();     myDispatchTimer.Tick -= new EventHandler(Initial_Wait);     myDispatcherTimer.Interval = TimeSpan.FromSeconds(interval); //every x seconds     myDispatcherTimer.Tick += new EventHandler(Each_Tick);     myDispatcherTimer.Start(); } 

The timer calls Initial_Wait the first time it ticks. That method stops the timer, redirects it to Each_Tick, and adjusts the interval. All subsequent ticks will go to Each_Tick.

If you want the timer to stop after 60 seconds, create a Stopwatch when you first start the timer, then check the Elapsed value with every tick. Like this:

Modify the InitialWait method to start the Stopwatch. You'll need a class-scope variable:

private Stopwatch _timerStopwatch;  void Initial_Wait(object o, EventArgs sender) {     // changing the timer here     // Now create the stopwatch     _timerStopwatch = Stopwatch.StartNew();     // and then start the timer     myDispatchTimer.Start(); } 

And in your Each_Tick handler, check the elapsed time:

if (_timerStopwatch.Elapsed.TotalSeconds >= 60) {     myDispatchTimer.Stop();     myDispatchTimer.Tick -= new EventHandler(Each_Tick);     return; } // otherwise do the regular stuff. 
by : Jim Mischelhttp://stackoverflow.com/users/56778




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog