Pausing and Resuming Threads cause High CPU Utilization

G

Guest

I have implemented Thread.Suspend and Thread.resume for pausing and resuming
threads respectively.
It works fine,But the only thing that I noticed was,when I suspend my
thread,the CPU utilization goes to 99-100%
Why would this happen?And is there a workaround for this or is there any
other way to pause and resume threads in .NET.
 
J

Jon Skeet [C# MVP]

MVB said:
I have implemented Thread.Suspend and Thread.resume for pausing and
resuming threads respectively. It works fine,But the only thing that
I noticed was,when I suspend my thread,the CPU utilization goes to
99-100% Why would this happen?And is there a workaround for this or
is there any other way to pause and resume threads in .NET.

It sounds quite strange, to be honest. However, I wouldn't use Suspend
and Resume myself. I'd use Monitor.Wait and Monitor.Pulse, or
Auto/ManualResetEvent.

See http://www.pobox.com/~skeet/csharp/threads for details.
 
W

Willy Denoyette [MVP]

MVB said:
I have implemented Thread.Suspend and Thread.resume for pausing and
resuming
threads respectively.
It works fine,But the only thing that I noticed was,when I suspend my
thread,the CPU utilization goes to 99-100%
Why would this happen?And is there a workaround for this or is there any
other way to pause and resume threads in .NET.

Do as Jon said, don't use Resume and Suspend they are obslete in v2.0 and
dangerous when used from within managed code.

Willy.
 
G

Guest

hi

what i want to do is..
I have a application which performs copying of files and folders from
several machines on the networks.When the network connectivity of a
particular machine is disconnected for some reason , i want to pause the
thread until the network connectivity is regained and when the network is
connected back,resume the thread from where it was paused.
In the meanwhile if there are other threads which are performing the copy on
different machine should continue un-interrupted.
Do you think monitor.wait() will help me achieve this.
Can you provide me with some kind of algorithm , if possible, to make things
more clear?

Thanks,
 
J

Jon Skeet [C# MVP]

MVB said:
what i want to do is..
I have a application which performs copying of files and folders from
several machines on the networks.When the network connectivity of a
particular machine is disconnected for some reason , i want to pause the
thread until the network connectivity is regained and when the network is
connected back,resume the thread from where it was paused.
In the meanwhile if there are other threads which are performing the copy on
different machine should continue un-interrupted.
Do you think monitor.wait() will help me achieve this.
Can you provide me with some kind of algorithm , if possible, to make things
more clear?

Basically, wherever you were using Thread.Suspend, use Monitor.Wait on
the thread which needs to pause. When you wish to resume it, call
Monitor.Pulse from another thread.
 
G

Guest

I tried using Monitor.wait(Thread that needs to be paused),But I get the
following error,Object synchronization method was called from an
unsynchronized block of code.

What would be the reason?

Thanks
 
J

Jon Skeet [C# MVP]

MVB said:
I tried using Monitor.wait(Thread that needs to be paused),But I get the
following error,Object synchronization method was called from an
unsynchronized block of code.

What would be the reason?

I suggest you read up on Monitor.Wait - I don't think it does what you
think it does. If you read the docs for Monitor.Wait you'll find out
why you got that exception. Hint: Monitor.Wait only ever makes the
current thread pause, not a different thread.
 

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