473,836 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(50 0)? 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.AddS econds(iNumSeco nds)
End If
Threading.Threa d.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Threa d.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 3491
AFAIK, the difference is that the Thread.Sleep(0) will start when its turn
to execute comes around again, where the Thread.Sleep(50 0) 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.Doe vents inside of DoWork, or run DoWork inside of a seperate
thread.

Chris

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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(50 0)? 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.AddS econds(iNumSeco nds)
End If
Threading.Threa d.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Threa d.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.goo glegroups.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(50 0)? 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.goo glegroups.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(50 0)? 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.AddS econds(iNumSeco nds)
End If
Threading.Threa d.Sleep(500)
Loop Until bStopRequest

Would I be better off using Threading.Threa d.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 ManualResetEven t.

Something like:

Dim bStopRequest As New ManualResetEven t(False)

Do
DoWork()
Loop Until bStopRequest.Wa itOne(x, False)

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

bStopRequest.Se t()

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.goo glegroups.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 ManualResetEven t 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.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
2915
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
2389
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
2796
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
5440
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
5255
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.
6
2824
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
2702
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
7004
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
3377
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
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9671
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
10846
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7793
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
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4458
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
2
4021
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
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.