Implementing a loop using a timer in C#
Asked Answered
W

4

12

I wanted to replace a counter based while loop with the timer based while loop in C#.

Example :

while(count < 100000)
{
   //do something 
}

to

while(timer < X seconds)
{
    //do something 
}

I have two types of timers in C# .NET for this System.Timers and Threading.Timers . Which one will be better to use and how.I don't want to add extra time consumption or threading issues with the timer.

Wooded answered 2/7, 2013 at 6:29 Comment(1)
possible duplicate of How to execute the loop for specific timeBaynebridge
M
13

Use a construct like this:

Timer r = new System.Timers.Timer(timeout_in_ms);
r.Elapsed += new ElapsedEventHandler(timer_Elapsed);
r.Enabled = true;
running = true;
while (running) {
   // do stuff
}
r.Enabled = false;

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
   running = false;
}

Be careful though to do this on the UI thread, as it will block input.

Malanie answered 2/7, 2013 at 6:34 Comment(2)
it has to be used in a console app.Will this be a problem !Wooded
@Wooded I don't think so. Try it and find out :).Malanie
L
32

What about using the Stopwatch class.

using System.Diagnostics;
//...
Stopwatch timer = new Stopwatch();
timer.Start();
while(timer.Elapsed.TotalSeconds < Xseconds)
{
    // do something
}
timer.Stop();
Lurie answered 2/7, 2013 at 6:33 Comment(0)
M
13

Use a construct like this:

Timer r = new System.Timers.Timer(timeout_in_ms);
r.Elapsed += new ElapsedEventHandler(timer_Elapsed);
r.Enabled = true;
running = true;
while (running) {
   // do stuff
}
r.Enabled = false;

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
   running = false;
}

Be careful though to do this on the UI thread, as it will block input.

Malanie answered 2/7, 2013 at 6:34 Comment(2)
it has to be used in a console app.Will this be a problem !Wooded
@Wooded I don't think so. Try it and find out :).Malanie
H
8

You can use Stopwatch class instead of them, like;

Provides a set of methods and properties that you can use to accurately measure elapsed time.

Stopwatch sw = new Stopwatch();
sw.Start();

while (sw.Elapsed < TimeSpan.FromSeconds(X seconds)) 
{
   //do something
}

From TimeSpan.FromSecond

Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.

Hothead answered 2/7, 2013 at 6:33 Comment(0)
S
3

You might as well use the DateTime.Now.Ticks counter:

long start = DateTime.Now.Ticks;
TimeSpan duration = TimeSpan.FromMilliseconds(1000);
do
{
  //
}
while (DateTime.Now.Ticks - start < duration);

However, this seems to be something like busy waiting. That means that the loop will cause one core of your CPU to run at 100%. It will slow down other processes, speed up fans a.s.o. Although it depends on what you intend to do I would recommend to include Thread.Sleep(1) in the loop.

Saharan answered 2/7, 2013 at 7:11 Comment(1)
Wow great solution. Actually I was trying to solve the cpu issue which you perfectly described in here. Thanks a lot, I was fascinated. RegardsSauter

© 2022 - 2024 — McMap. All rights reserved.