Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read an article on the CodeProject thread work with but one question is how to pause a thread in a time and then run it again?
EX: when you click a button to pause the program again, and when you press the button again, then the program continues
thanks!
Posted
Updated 10-Jun-11 21:42pm
v3
Comments
[no name] 11-Jun-11 15:36pm    
How is about implement something like StopMe and ResumeMe for your thread, and this mehtods controls then the thread state?

The operation System.Threading.Thread.Suspend and Resume are now considered obsolete, for a good reason, as these operations are dangerous. See http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].

Read this from http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx[^]:
"Caution

Do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily."
Instead, a thread should be suspended or resumed using the synchronization primitive System.Threading.EventWaitHandle. The instance of this class should be used in certain point in code by a working thread in a call to EventWaitHandle.WaitOne. If this instance is not set, the thread is put in a wait state by the OS: it is switched off and never scheduled back to execution until awaken (so, in this state is uses zero processor time). It can be awaken if some other thread calls EventWaitHandle.Set on the same instance. Other condition for wake-up include timeout of System.Threading.EventWaitHandle.WaitOne call (if applied), or a call the Thread.Abort or Thread.Interrupt called in some other thread. These are the ways to resume the thread from the blocking call to System.Threading.EventWaitHandle.WaitOne.

One important application of this method is the Blocking Queue, I provide the full source code in my Tips/Tricks article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

See also my other answers on related topics and links to other past answers:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
v2
Comments
RaviRanjanKr 12-Jun-11 1:14am    
Great Answer SA, My 5 :)
Sergey Alexandrovich Kryukov 12-Jun-11 1:15am    
Thank you, Ravi.
--SA
Tarun.K.S 12-Jun-11 13:38pm    
Good answer SA. 5+
Sergey Alexandrovich Kryukov 12-Jun-11 13:43pm    
Thank you, Tarun.
--SA
 
Share this answer
 
You may 'notify' the worker thread it should pause o resume by setting a variable value, see, for instance, the following page: How to Pause or sleep the background worker thread[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jun-11 23:51pm    
Sorry, this link is not very good. Suspend/Resume are obsolete methods because they are dangerous. It needs explanation. Most answers in this link fail to explain it, it is only mentioned at the end. Besides, there is no sufficient explanations and no alternative solution (indeed, some method of pausing/resuming is really needed, what to do?)
(I did not vote).

All this one can find in my answer, please see.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900