473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Alternative to Thread.Sleep?

Hi,

I have some things that act in a typical producer consumer fashion.

When they have work to do, I want them doing that, but if there's no
work, I'm currently using Thread.Sleep to cause the thread to sleep so
its not continuously eating CPU time.

Is there another alternative to keep the thread from running? These
threads are used in a windows service, and when the service is stopped
it takes longer than I'd like it to because the thread sleeps for
about 15 seconds, then checks if it should be running.

Thanks
Andy

Aug 2 '07 #1
9 15027
On Aug 2, 2:15 pm, Andy <an...@med-associates.comwrote:
I have some things that act in a typical producer consumer fashion.

When they have work to do, I want them doing that, but if there's no
work, I'm currently using Thread.Sleep to cause the thread to sleep so
its not continuously eating CPU time.

Is there another alternative to keep the thread from running? These
threads are used in a windows service, and when the service is stopped
it takes longer than I'd like it to because the thread sleeps for
about 15 seconds, then checks if it should be running.
Use Monitor.Wait instead, and call Monitor.Pulse or Monitor.PulseAll
to wake the thread up.

See http://pobox.com/~skeet/csharp/threads/deadlocks.shtml for an
example producer/consumer queue class.

Jon

Aug 2 '07 #2
Andy,

Why not use an event (ManualResetEvent, AutoResetEvent) which you wait
on to be signaled when there is work to be done (or when the service is
shutting down)?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
Hi,

I have some things that act in a typical producer consumer fashion.

When they have work to do, I want them doing that, but if there's no
work, I'm currently using Thread.Sleep to cause the thread to sleep so
its not continuously eating CPU time.

Is there another alternative to keep the thread from running? These
threads are used in a windows service, and when the service is stopped
it takes longer than I'd like it to because the thread sleeps for
about 15 seconds, then checks if it should be running.

Thanks
Andy
Aug 2 '07 #3
Hi,

What if you mark your thread as IsBackground, I'm under the impression that
under this escenario the thread is just aborted.

"Andy" <an***@med-associates.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
Hi,

I have some things that act in a typical producer consumer fashion.

When they have work to do, I want them doing that, but if there's no
work, I'm currently using Thread.Sleep to cause the thread to sleep so
its not continuously eating CPU time.

Is there another alternative to keep the thread from running? These
threads are used in a windows service, and when the service is stopped
it takes longer than I'd like it to because the thread sleeps for
about 15 seconds, then checks if it should be running.

Thanks
Andy

Aug 2 '07 #4
As others have already pointed out, either a Monitor or an Event is a much
better solution than a Sleep. Either will allow your thread(s) to wake up
when there is work to be done.

You might want to take a look at Duffy's "Blocking Queue", as it's likley
very close to what you need.
http://msdn.microsoft.com/msdnmag/is...efault.aspx#S4

I would also recommending reading all the algorithms he lays out in that
article, as the education is well worth the time...

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Andy" <an***@med-associates.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
Hi,

I have some things that act in a typical producer consumer fashion.

When they have work to do, I want them doing that, but if there's no
work, I'm currently using Thread.Sleep to cause the thread to sleep so
its not continuously eating CPU time.

Is there another alternative to keep the thread from running? These
threads are used in a windows service, and when the service is stopped
it takes longer than I'd like it to because the thread sleeps for
about 15 seconds, then checks if it should be running.

Aug 2 '07 #5
On Aug 2, 9:29 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
Use Monitor.Wait instead, and call Monitor.Pulse or Monitor.PulseAll
to wake the thread up.

Seehttp://pobox.com/~skeet/csharp/threads/deadlocks.shtmlfor an
example producer/consumer queue class.
Jon,

Yes, I started investigating that and did a test implementation. The
only concern I had was that I need to Enter and Exit the monitor,
although technically there was nothing that I was really locking
against. It seems though that this is the correct way to go, and I've
been happy with how it is functioning.

Aug 2 '07 #6
On Aug 2, 10:16 am, "Ignacio Machin \( .NET/ C# MVP \)"
<ignacio.machin AT dot.state.fl.uswrote:
What if you mark your thread as IsBackground, I'm under the impression that
under this escenario the thread is just aborted.
The thread's are background threads already, but I wanted to be able
to pause execution as well. Also, the processing is such that I
didn't want the possiblity of the thread dying in the middle of the
processing code because the foreground thread terminated.

Thanks
Andy

Aug 2 '07 #7
On Aug 2, 9:32 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Why not use an event (ManualResetEvent, AutoResetEvent) which you wait
on to be signaled when there is work to be done (or when the service is
shutting down)?
I looked at this as well, but I wasn't sure how to apply it properly
to my scenario. I think I'll stick with the Monitor though, because
its only a few lines of code.

Thanks for the feedback!
Andy

Aug 2 '07 #8
Andy <an***@med-associates.comwrote:
On Aug 2, 9:29 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
Use Monitor.Wait instead, and call Monitor.Pulse or Monitor.PulseAll
to wake the thread up.

Seehttp://pobox.com/~skeet/csharp/threads/deadlocks.shtmlfor an
example producer/consumer queue class.

Yes, I started investigating that and did a test implementation. The
only concern I had was that I need to Enter and Exit the monitor,
although technically there was nothing that I was really locking
against.
You're locking against the monitor. Use the "lock" keyword instead of
manually calling Monitor.Enter/Exit to make it more robust (it's an
automatic try/finally).
It seems though that this is the correct way to go, and I've
been happy with how it is functioning.
Goodo.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '07 #9
Hi,

"Andy" <an***@med-associates.comwrote in message
news:11*********************@q3g2000prf.googlegrou ps.com...
On Aug 2, 10:16 am, "Ignacio Machin \( .NET/ C# MVP \)"
<ignacio.machin AT dot.state.fl.uswrote:
>What if you mark your thread as IsBackground, I'm under the impression
that
under this escenario the thread is just aborted.

The thread's are background threads already, but I wanted to be able
to pause execution as well. Also, the processing is such that I
didn't want the possiblity of the thread dying in the middle of the
processing code because the foreground thread terminated.
If the threads are marked as Background they will die as soon as the
foreground thread dies.
Aug 2 '07 #10

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

Similar topics

38
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...
26
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...
8
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...
4
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...
9
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...
6
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...
0
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...
12
by: gagonm | last post by:
HI I am working on Engineering application where we need thread.sleep() function many times. But since sleep interval is not accurate we need an alternative which can mimick functionality of...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.