How to Sleeping a thread for Zero Seconds in C#

Posted By  j2inet On 17 Jul 2010 23:07:47
emailbookmarkadd commentsprint
No of Views:3552
Bookmarked:0 times
Votes:0 times

Introduction

I was asked about something that appeared to be an oddity in some example code that I posted online. The code in question was a single call to the sleep function.

Thread.Sleep(0);

 or if you prefer to see the native code version:

Sleep(0);

 The sleep function is often used to insert delays into code. But what does it mean to insert a delay of zero? This will make more sense if we first take a look at the API documentation. I pulled this description from the native API documentation for the sleep function:

This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds.

So there are two actions performed by calling this function:

  1. Thread relinquishes its time slice
  2. Thread becomes unrunnable for the interval specified

Sleeping for zero seconds will cause the thread to relinquish control, but it will remain runnable. What does it mean for a thread to be runnable? To understand this, one must understand how multitasking works. At its simplest level, a single core processor can only run one thread or program at a time. The illusion of multiple programs doing something at the same time is maintained by rapidly switching control from one program to another. When the operating system is deciding which thread to switch to next one of the factors is which threads are runnable. A thread can become unrunnable voluntarily (by calling the sleep function or using other thread related APIs) or the thread may make a call that requires some time for an event to complete, such as an IO request to a drive or network device. When the thread is made runnable again then it is reintroduced as a candidate thread for receiving time to execute.

When a program calls Sleep(0) it is never made unrunnable. It just allows for the operating system to go ahead and schedule the next thread for execution.

When is it Appropriate to Use Sleep(0)?

Most often I use Sleep(0) in code in which the main program loop is not being driven by messages and events. For programs that are driven by messages and events if the program is not receiving any messages (such as notification of user actions, GPS coordinates changing, timers expiring, so on) then the program is not doing anything. Programs of this nature spend a majority of their life not doing anything. Though the user typically won't perceive these programs that way. Programs that are not dependent on messages and events to run (typically games or media intensive applications) execute continuously and can quickly consume CPU bandwith. Throwing the Sleep(0) into the main loop of such a program will give other programs a chance to execute (as opposed to appearing locked up due to another process monopolizing the CPU) and in some cases the Sleep(0) can help better preserve battery life.

Sign Up to vote for this article
Other popular Tips/Tricks
    In this tip, I will explain how to use the BigInteger in C# 4.0.The BigInteger is new data type in .NET 4.0.
    Published Date : 22/Sep/2011
    In this tips,I am going to discuss about two important thing about Split function of String class. Split function of the string class split the string in array of string.
    Published Date : 29/Jul/2011
    The Facebook is very popular and it has great support to other development Languages by Facebook SDKs.In this tips, I going to explain how to upload Videos to Facebook through C# with few lines of code.
    Published Date : 13/Jun/2011
    In this tip, i will share with you , how to format time HH:MM:SS from seconds in C#.
    Published Date : 25/Feb/2011
    Enums in dot net programming is a great facility and we all used it to increase code readability. In earlier version of .NET framework we don’t have any method anything that will check whether a value is assigned to it or not. In C# 4.0 we have new static method called HasFlag which will check that particular value is assigned or not.
    Published Date : 01/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top