473,651 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread.Sleep(0)

This is from MSDN online
(http://msdn2.microsoft.com/en-us/lib...00bd51t.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.
Sep 9 '07 #1
4 11061
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)?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Frankie" <A@B.COMwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
This is from MSDN online
(http://msdn2.microsoft.com/en-us/lib...00bd51t.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.

Sep 9 '07 #2
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/lib...ion.post.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


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:D8******** *************** ***********@mic rosoft.com...
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)?

<snip>
Sep 9 '07 #3
"Frankie" <A@B.COMwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
This is from MSDN online
(http://msdn2.microsoft.com/en-us/lib...00bd51t.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.

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.

Sep 9 '07 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Frankie" <A@B.COMwrote in message
news:Os******** ******@TK2MSFTN GP03.phx.gbl...
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/lib...ion.post.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


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:D8******** *************** ***********@mic rosoft.com...
>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)?


<snip>
Sep 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
2876
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in late July. A number of obscure crash-causing bugs have been fixed, various memory leaks have been squished, but no new features have been added to the language or to the library. For more information on Python 2.3.1, including download links for...
26
2371
by: news.microsoft.com | last post by:
Hi, Currently I have a thread thats spinning and doing a Thread.Sleep(someTime). I was thinking of changing this to Thread.Sleep(Timeout.Infinite); then when I have actual data in a collection to process in this thread, I was going to do a _thread.Interrupt(); Would this be a better approach to spinning a thread on ?
8
2777
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the routine that loops through the file buffer: for (int i=0;i < _Buffer.length; i++) { // Code here
4
5428
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
1
5242
by: Matthijs | last post by:
Hi, I have a problem coding a UDP Server/client. The server needs to send big amounts of data over UDP. The problem however is that you can't send as fast as you like. (All packets will be dropped due to the fact that a router can't handle the huge number of packets (most likely the first one the packets hit)). To fix this problem you need to increase/decrease the speed according to packetloss.
9
3482
by: Chris Dunaway | last post by:
According to the docs, calling Thread.Sleep(0) causes the thread to be "suspended to allow other waiting threads to execute." What happens if I call Thread.Sleep(500)? Do other threads not get a chance to execute during this time? What is the difference between the two? I have code that runs in a loop like this: Dim dResetTime As DateTime = DateTime.Now
6
2811
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second adds another dot to the label). This works great. However, when I put the GUI thread to sleep (Thread.Sleep), the thread seems to stall. At first I was expecting dots to still appear, but obviously as the GUI thread is asleep, they will not. ...
0
2678
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the problem might be the way I'm passing the value. can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong. Please note there are 3 separate files with the corresponding classes, I separated it with ===>...
9
6973
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference, but the other items are passed normally.
2
3360
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always sitting in the sleep command and not able to be interrupted. When the time came to set the semaphore flag to false (stopping the thread), my program would have to wait up to the entire sleep time to break out of the loop. I have finally found...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.