Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Thread.Sleep(0)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frankie - 09 Sep 2007 15:05 GMT
This is from MSDN online
(http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx):
"Specify zero (0) to indicate that this thread should be suspended to allow
other waiting threads to execute."

My questions:

1. By "other threads" - does the above statement refer specifically and only
to other threads in the current AppDomain?

2. When would execution continue in the current thread (after a thread hits
Thread.Sleep(0))? Is it after all other threads - regardless of their
priority - have run their course completely, or is it after only higher
priority threads have run their course?

Just looking to understand the implications of adding Thread.Sleep(0) to my
code - and when doing so would be a good thing [or possibly bad thing] to
do.

Thanks.
Nicholas Paldino [.NET/C# MVP] - 09 Sep 2007 15:45 GMT
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>
Willy Denoyette [MVP] - 09 Sep 2007 16:55 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.