How do I put to sleep a thread other than the current one.

P

Peter Rilling

How do I put to sleep a thread other than the current one?

I want to put another thread to sleep to the specified number of
milliseconds but the Sleep method seems to only work for the current thread
while the Suspend method has no parameters to specify a duration.
 
W

Wiktor Zychla

I want to put another thread to sleep to the specified number of
milliseconds but the Sleep method seems to only work for the current thread
while the Suspend method has no parameters to specify a duration.

I am afraid that this is not possible at all. for the thread to sleep it
must be active at the moment you invoke the sleep function (otherwise it is
senseless because the thead IS sleeping!).

what you want to do is not to call Sleep for the other thread but rather to
block its execution for some time. to accomplish that you should use any
object that can synchronize threads, for example Mutexes.

Regards,
Wiktor Zychla
 
R

Rene

I am afraid that this is not possible at all. for the thread to sleep it
must be active at the moment you invoke the sleep function (otherwise it is
senseless because the thead IS sleeping!).

What if I have a dual processor? Can't processor 1 and 2 be running a
different thread at the same time?
 
D

Dave

Rene said:
What if I have a dual processor? Can't processor 1 and 2 be running a
different thread at the same time?
Sure, but you still need to use the correct synchronization mechanism. In
almost all cases you should use a sync object, e.g. ManualResetEvent, and
the thread blocks on that object until some other thread sets the object to
the signalled state, rather then directly Suspend/Resume a 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