471,602 Members | 1,239 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

Thread.Sleep

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
Do
If DateTime.Now >= dResetTime Then
DoWork()
dResetTime = dResetTime.AddSeconds(iNumSeconds)
End If
Threading.Thread.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Thread.Sleep(0) in this instance?
How would this affect CPU load? I want the DoWork method to run with
an interval of iNumSeconds but I want to be able to stop the loop by
setting the bStopRequest to True.

Is this an appropriate to handle this? I don't want the loop to tax
the CPU.

Thanks for any insight or suggestions.

Chris

Nov 21 '05 #1
9 3350
AFAIK, the difference is that the Thread.Sleep(0) will start when its turn
to execute comes around again, where the Thread.Sleep(500) will execute when
500 ms has passed and its turn comes around to execute again. Basically the
500 makes sure the thread waits at least 500 ms while the 0 says, someone
else just take a turn.

If you want DoWork to run every X seconds, you might want to do something
like:

Do
DoWork
thread.sleep(X)
Loop Until bStopRequest

This way the thread will sleep for X seconds then start DoWork again. Now
the UI will not be responsive while the thread is sleeping. If you need a
UI, you may want to look at a Timer instead. It would be a better solution.

If you want to make sure your processor isn't super taxed during the
execution of DoWork, you need to do one of two things. Either do an
Application.Doevents inside of DoWork, or run DoWork inside of a seperate
thread.

Chris

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Do
If DateTime.Now >= dResetTime Then
DoWork()
dResetTime = dResetTime.AddSeconds(iNumSeconds)
End If
Threading.Thread.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Thread.Sleep(0) in this instance?
How would this affect CPU load? I want the DoWork method to run with
an interval of iNumSeconds but I want to be able to stop the loop by
setting the bStopRequest to True.

Is this an appropriate to handle this? I don't want the loop to tax
the CPU.

Thanks for any insight or suggestions.

Chris

Nov 21 '05 #2
Thanks for the response,
If you want DoWork to run every X seconds, you might want to do something like:

Do
DoWork
thread.sleep(X)
Loop Until bStopRequest


I started with this, but if X is a large interval, such as a minute or
two, then the thread will be blocked and the bStopRequest will only be
tested when it unblocks. I want it to remain responsive so that if the
bStopRequest is set to True, then it will stop and not have to wait
until the interval expires.

Chris

Nov 21 '05 #3
Use a Timer then. This fires off an event every Timer.Interval
milli-seconds. This will allow your form to be responsive.

Chris

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks for the response,
If you want DoWork to run every X seconds, you might want to do

something
like:

Do
DoWork
thread.sleep(X)
Loop Until bStopRequest


I started with this, but if X is a large interval, such as a minute or
two, then the thread will be blocked and the bStopRequest will only be
tested when it unblocks. I want it to remain responsive so that if the
bStopRequest is set to True, then it will stop and not have to wait
until the interval expires.

Chris

Nov 21 '05 #4
Chris,
What happens if I call Thread.Sleep(500)? Causes your thread to wait about 500 milliseconds before it resumes.
What is the difference between the
two? Thread.Sleep(0) gives up your current "time slice", if at the next time
slice you are the highest priority thread, you will regain control of the
CPU. A time slice is normally a fraction of a second, I believe it may even
be a fraction of a millisecond... Unfortunately I don't have a reference
handy on how long a time slice normally is...

Windows will automatically dynamically raise & lower priorities so threads
are not denied any time slices.

I normally use Sleep(0), unless I don't want to starve any UI threads...

Hope this helps
Jay

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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
Do
If DateTime.Now >= dResetTime Then
DoWork()
dResetTime = dResetTime.AddSeconds(iNumSeconds)
End If
Threading.Thread.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Thread.Sleep(0) in this instance?
How would this affect CPU load? I want the DoWork method to run with
an interval of iNumSeconds but I want to be able to stop the loop by
setting the bStopRequest to True.

Is this an appropriate to handle this? I don't want the loop to tax
the CPU.

Thanks for any insight or suggestions.

Chris

Nov 21 '05 #5
Chris,
Rather then use a Boolean & Thread.Sleep, consider using a ManualResetEvent.

Something like:

Dim bStopRequest As New ManualResetEvent(False)

Do
DoWork()
Loop Until bStopRequest.WaitOne(x, False)

When another thread wants to stop the thread "Set" bStopRequest, like:

bStopRequest.Set()

If bStopRequest is Set, then WaitOne will return True immediately, if
bStopRequest stays Reset (is not Set), then WaitOne will wait the full time
interval before returning False.

Hope this helps
Jay
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks for the response,
If you want DoWork to run every X seconds, you might want to do

something
like:

Do
DoWork
thread.sleep(X)
Loop Until bStopRequest


I started with this, but if X is a large interval, such as a minute or
two, then the thread will be blocked and the bStopRequest will only be
tested when it unblocks. I want it to remain responsive so that if the
bStopRequest is set to True, then it will stop and not have to wait
until the interval expires.

Chris

Nov 21 '05 #6
Thanks Jay,

I had recently used a ManualResetEvent elsewhere and it did not occur
to me to use it in this case. I think it will work nicely.

Chris

Nov 21 '05 #7
Chris,

As I understand you well, do I in this situation raise an public event in
the worker thread when it is ready and catch that in the mainthread.

When a thread is doing nothing than it is not needed to stop it.

I hope this helps?

Cor
Nov 21 '05 #8
Jay, can you explain what the second argument to WaitOne means, the
docs are not clear to me.

In what situation would I ever use True on the WaitOne method?

Thanks again

Nov 21 '05 #9
Chris,
Unfortunately other then those docs I have not come across it, I normally
use False, as that is what WaitOne() (no parameters) uses. I've seem many an
example that uses True, however the ones I've checked do not include a
reason.

Hope this helps
Jay

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Jay, can you explain what the second argument to WaitOne means, the
docs are not clear to me.

In what situation would I ever use True on the WaitOne method?

Thanks again

Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Anthony Baxter | last post: by
26 posts views Thread by news.microsoft.com | last post: by
8 posts views Thread by Cider123 | last post: by
4 posts views Thread by Matthew Groch | last post: by
1 post views Thread by Matthijs | last post: by
6 posts views Thread by k.mellor | last post: by
9 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
2 posts views Thread by Steve | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by CCCYYYY | last post: by

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.