Frankie,
1. By other threads, it does not mean other threads in the app domain. It
means any other thread that is waiting that is of equal priority (see
http://msdn2.microsoft.com/en-us/library/ms686307.aspx for more details, as
SleepEx is what is ultimately called).
2. It's not guaranteed when your thread will regain control. That's up to
the scheduler on the OS to decide.
Why is it that you want to use Sleep(0)?

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> This is from MSDN online
> (http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx):
[quoted text clipped - 16 lines]
>
> Thanks.
Frankie - 09 Sep 2007 16:20 GMT
Thanks Nicholas... RE:
<< Why is it that you want to use Sleep(0)? >>
I saw it used in sample code while researching the AsyncOperation.Post
method (at
http://msdn2.microsoft.com/en-us/library/system.componentmodel.asyncoperation.po
st.aspx).
The sample code at that link includes the following comment and call to
Thread.Sleep:
// Yield the rest of this time slice.
Thread.Sleep(0);
It makes sense to me, in this context, to have a thread .Sleep(0) when it's
done doing its "important" and potentially processor-intensive work... so
that other threads - presumably initiated by the current app's
other/concurrent async operations - can do their "important" work. then come
back later to do cleanup when the thread schedule gets around to it. But all
of this rationalle I just stated is my best guess as to why Thread.Sleep(0)
was put in there by the authors of the sample code.
Frankie
> Frankie,
>
[quoted text clipped - 7 lines]
>
> Why is it that you want to use Sleep(0)?
<snip>
Nicholas Paldino [.NET/C# MVP] - 09 Sep 2007 16:59 GMT
Frankie,
To be honest, I think that it shouldn't be in the sample code, as I
would consider that a premature optimization. Generally, you are using
threads already to handle the processing of other units of work
concurrently. If you want to give up your processing time slice in general,
then you should make a call to another asynchronous method.
I think that Sleep(0) is one of those things that should be used after
you have finished the code, and through profiling you realize that you are
hogging up too much processor time.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Thanks Nicholas... RE:
> << Why is it that you want to use Sleep(0)? >>
[quoted text clipped - 31 lines]
>
> <snip>
> This is from MSDN online
> (http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx):
[quoted text clipped - 16 lines]
>
> Thanks.
Be careful with Thread.Sleep(0), read the Community Content at the bottom of
the above msdn page.
There is IMO no good reason to call this API with a 0 msec. time-out,
especially not when called in a tight loop.
Willy.