By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

Alternative to Thread.Sleep?

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.