Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a loop using a timer in C#

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.

like image 898
Raulp Avatar asked Jul 02 '13 06:07

Raulp


People also ask

Is there a timer in C?

You can't have timers in pure standard C.

HOW IS FOR loop executed in C?

In for loop, a loop variable is used to control the loop. First, initialize this loop variable to some value, then check whether this variable is less than or greater than the counter value. If the statement is true, then the loop body is executed and the loop variable gets updated.

What is loop in C programming with example?

What are Loops in C? Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times so it saves code and also helps to traverse the elements of an array.


4 Answers

What about using the Stopwatch class.

using System.Diagnostics;
//...
Stopwatch timer = new Stopwatch();
timer.Start();
while(timer.Elapsed.TotalSeconds < Xseconds)
{
    // do something
}
timer.Stop();
like image 196
Casperah Avatar answered Oct 12 '22 13:10

Casperah


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.

like image 39
Bart Friederichs Avatar answered Oct 12 '22 12:10

Bart Friederichs


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.

like image 8
Soner Gönül Avatar answered Oct 12 '22 14:10

Soner Gönül


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.

like image 2
JeffRSon Avatar answered Oct 12 '22 12:10

JeffRSon