Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time Delay in C#

Tags:

c#

.net

The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value as a parameter. Is there any method of thread delay in microsecond. Or can sleep function take the float values? Thanks

like image 830
Siddiqui Avatar asked May 24 '09 12:05

Siddiqui


People also ask

What is time delay in C programming?

Delay in C: delay function is used to suspend execution of a program for a particular time. Declaration: void delay(unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the "dos.

What is sleep () in C?

The function sleep gives a simple way to make the program wait for a short interval. If your program doesn't use signals (except to terminate), then you can expect sleep to wait reliably throughout the specified interval.

What library is delay in C?

The delay() function is built upon a C library function called clock(). The clock() function returns a time value in clock ticks, which is based on the processor's speed. The value returned is of the clock_t variable type.

Which loop is used for time delay?

In electronics, a delay-locked loop (DLL) is a pseudo-digital circuit similar to a phase-locked loop (PLL), with the main difference being the absence of an internal voltage-controlled oscillator, replaced by a delay line.


2 Answers

Nope. To quote Will Dean from 85122

you can't meaningfully 'sleep' (i.e. relinquish your scheduled CPU) for such short periods. If you want to delay for some short time, then you need to spin, repeatedly checking a suitably high-resolution timer (e.g. the 'performance timer') and hoping that something of high priority doesn't pre-empt you anyway.

like image 140
Dan F Avatar answered Oct 25 '22 19:10

Dan F


Sleep is not accurate to the millisecond in the first place. I think it's resolution is hardware dependent, and the smallest slice of time you can sleep will typically be around 20 ms. This has to do with Sleep actually releases what might be remaining of the current thread's timeslice; and allows another thread to run. The earliest time your thread will be able to run again, is after one thread scheduler timeslice has passed. Therefore the resolution is about 20 ms (assuming a timeslice on your system is 20 ms).

Since Windows is not a real-time OS; Sleep and other wait functions can never be fully deterministic.

Depending on your application, you could perhaps do a Sleep for the most of the time and then do a busy wait where timing is critical. Or rewrite the structure so that you measure time passed accurately (use System.Diagnostics.Stopwatch); but do not depend on being able to sleep for an accurate time period in the millisecond range.

The first answer in this thread on MSDN also explains it very nicely.

like image 26
driis Avatar answered Oct 25 '22 20:10

driis