Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is .NET Thread.Sleep affected by DST (or system time) changes?

i'm not sure how the Windows kernel handles Thread timing ...

i'm speaking about DST and any other event that affects the time of day on Windows boxes.

for example, Thread .Sleep will block a thread from zero to infinite milliseconds.

if the kernel uses the same "clock" as that used for time of day, then when

(a) someone manually changes the time of day, or
(b) some synchronization to a time server changes the time of day, or
(c) Daylight Saving Time begins or ends and the system has been configured to respond to these two DST events,
et cetera,

are sleeping threads in any way affected? i.e., does the kernel handle such events in such a way that the programmer need do nothing?

N.B.: for non-critical applications, this is likely a who cares? situation.

For critical applications, knowing the answer to this question is important because of the possibility that one must program for such exception conditions.

thank you

edit: i thought of a simple test which i've run in LINQPad 4.

the test involved putting the thread to sleep, starting a manual timer at approximately the same time as the thread was put to sleep, and then (a) moving the time ahead one hour, then for the second test, moving the time back two hours ... in both tests, the period of sleep was not affected.

Bottom line: with Thread.Sleep, there is no need to worry about events that affect the time of day.

here's the trivial c# code:

Int32   secondsToSleep;
String  seconds; 
Boolean inputOkay;
Console.WriteLine("How many seconds?");
seconds   = Console.ReadLine();
inputOkay = Int32.TryParse(seconds, out secondsToSleep); 
if (inputOkay)
{
    Console.WriteLine("sleeping for {0} second(s)", secondsToSleep);
    Thread.Sleep(secondsToSleep * 1000);
    Console.WriteLine("i am awake!");
}
else Console.WriteLine("invalid input:  [{0}]", seconds);
like image 281
gerryLowry Avatar asked Sep 12 '12 19:09

gerryLowry


People also ask

How does thread sleep Work C#?

In C#, a Sleep() method temporarily suspends the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution. There are two methods in the overload list of Thread. Sleep Method as follows: Sleep(Int32)

What is the alternative for thread sleep?

Alternative: use wait() and notify() Now we have to refactor our test as well. After we start the counter we need to block the thread running the test until the counting is over.

What happens when thread sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Does thread sleep release lock C#?

No, the lock will not be released if you Sleep. If you want to release it, use Monitor. Wait(o, timeout) ; further, you can also use this to signal from another thread - another thread can use Monitor.


1 Answers

No, Thread.Sleep is not affected. The kernel furthermore keeps time in UTC exclusively. Time zones and DST are just layered above that.

like image 79
Joey Avatar answered Oct 13 '22 20:10

Joey