Alternative to Thread.Sleep?

A

Andy

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
 
J

Jon Skeet [C# MVP]

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
 
N

Nicholas Paldino [.NET/C# MVP]

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)?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

Chris Mullins [MVP]

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/issues/07/05/CLRInsideOut/default.aspx#S4

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

Andy

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.
 
J

Jon Skeet [C# MVP]

Andy said:
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.
 
A

Andy

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
 
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)?

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Andy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top