Thread.Sleep

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have C# application, in main form I made thead...
User click on susspend button which cause to suspend the thread, in the
button fuction:
mThread.Susspend();

What I should do to wait in this function untile the thread become
susspended ?
I tried the following ,but not work I tried Sleep but this cause to sleep
all the threads?, why?
mThread.Susspend();
do
{
if((mThread.ThreadState &
(System.Threading.ThreadState.Suspended))==Threading.ThreadState.Suspended)
{
break;
}
//Why this also Sleep all threads?????????
Thread.Sleep(200);
}
while (true);
 
Thread.Sleep only puts the current thread to sleep other threads continue to
run, but you call this in a loop on the main UI thread, that means you block
the message pump with as result:
1. A non respnsive UI.
2. Alternate threads cannot update the UI.

Willy.
 
I have C# application, in main form I made thead...
User click on susspend button which cause to suspend the thread, in the
button fuction:
mThread.Susspend();

Not a good idea, IMO.
What I should do to wait in this function untile the thread become
susspended ?

Instead of instructing other threads in that direct kind of wait, I'd
suggest using Monitor.Wait/Pulse or Auto/ManualResetEvents to signal
between the threads. One thread can then signal that it wants the other
to wait (for another signal) and that second thread can in return
signal that it's noticed, and is about to wait for the next signal.
 

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

Back
Top