Resuming C # Threads

Possible duplicate question: Is there a way to suspend the thread endlessly?


In my code I do below

Thread mainThread
//...
mainThread.Resume();

void StartThread()
{
    while (!wantQuit)
    {
        db.runEmail();
        mainThread.Suspend();
    }
}

Then I get the exception below because I call the resume when it has not been paused.

System.Threading.ThreadStateException

I noticed this warning

warning CS0618: 'System.Threading.Thread.Resume()' is obsolete: 'Thread.Resume 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'

So wondering, is there a way to resume and ignore the exception / case where it is not paused? I hate writing below, not just one line

        try
        {
            mainThread.Resume();
        }
        catch (System.Threading.ThreadStateException e)
        {
        }
+3
source share
4 answers

A simple solution is to correct your logic and not call Resume () when you are not paused ().

But the Resume / Suspend API is really deprecated, see for example:

1) Monitor , Wait () and Pulse ()

2) AutoResetEvent ManualResetEvent, Set() WaitOne()

Monitor lock() {}, ResetEvent, , "" . , Monitor.Pulse() , Wait() .


, , , , Suspend/.

+7

WaitHandles - .

, , . , "" .

. MSDN ManualResetEvent.

+2

In this case, you can take a look at using a thread monitor.

0
source

You can check Thread.ThreadState and check ThreadState. Suspension before renewal.

0
source

Source: https://habr.com/ru/post/1716714/


All Articles