replacement for Thread.Suspend(), Thread.Resume() ?
-
Hello, I am getting a C# compiler warning for using Thread.Suspend() and Thread.Resume() methods:
Warning 6 'System.Threading.Thread.Suspend()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in System.Threading,
such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect
resources. http://go.microsoft.com/fwlink/?linkid=14202'I'm implementing these thread methods in a safe environment, when only a single worker thread accesses the resources. This worker thread is performing a lenghty calculation so I wanted to have an option to suspend/resume this CPU intensive and lenghty calculation. Is there any other simple way to accomplish this task? Monitor, Mutex, Event nor Semaphore don't seem to be an effective replacement for Suspend() and Resume(). Thanks much, Michal Kreslik
-
Hello, I am getting a C# compiler warning for using Thread.Suspend() and Thread.Resume() methods:
Warning 6 'System.Threading.Thread.Suspend()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in System.Threading,
such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect
resources. http://go.microsoft.com/fwlink/?linkid=14202'I'm implementing these thread methods in a safe environment, when only a single worker thread accesses the resources. This worker thread is performing a lenghty calculation so I wanted to have an option to suspend/resume this CPU intensive and lenghty calculation. Is there any other simple way to accomplish this task? Monitor, Mutex, Event nor Semaphore don't seem to be an effective replacement for Suspend() and Resume(). Thanks much, Michal Kreslik
Thread.Sleep(Timeout.Infinite) should halt the thread. you can then interrupt it if you want it to start doing things again. HTH Russ
-
Thread.Sleep(Timeout.Infinite) should halt the thread. you can then interrupt it if you want it to start doing things again. HTH Russ
OK, thanks, but Sleep() is a static method, so it's not possible to call this method on any thread other than the current one, correct? That means that it's not possible to put a thread to sleep externally this way. Is that true or am I missing something? Thank you, Michal Kreslik
-
OK, thanks, but Sleep() is a static method, so it's not possible to call this method on any thread other than the current one, correct? That means that it's not possible to put a thread to sleep externally this way. Is that true or am I missing something? Thank you, Michal Kreslik
i think the aim of the changes to the resume and suspend methods was that code that executes outside a thread doesn't know what resources the thread is using. If for instance there was a database transaction open in the thread that you are putting to sleep you could create chaos for all users of the application not just the specific instance that the thread is running in. I think the general plan is to check for the state of a variable somewhere in your code
while(_running) {DoStuff();} else { ReleaseExpensiveResources(); Thread.Sleep(Timeout.Infinite); }
This way the thread won't suspend immedeately but it will suspend cleanly when it releases. Russ