Share via


Alternative for Resume() and Suspend () Methods in Thread.

Question

Thursday, August 30, 2012 5:45 AM

By using these tw0 methods in my application i can pause and resume the current thread.

but i get warnings as 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.

is there any alternative way to do this resume and suspend() methods in Thread class!?

I so please provide me an example.

All replies (16)

Thursday, August 30, 2012 5:48 AM ✅Answered | 2 votes

This entirely depends on why you are using suspend/resume in the first place. There's no general alternative because the problem is not that these functions are buggy or something like that, the overall idea of suspending a thread is usually bad.


Thursday, August 30, 2012 7:50 AM

You can redesign your thread (which probably represents a loop) so that it does the job only when a Boolean flag is true. For example you can check it at the beginning of the loop, or in other several places. The role of this flag can be played by suggested event objects, such as ‘ManualResetEvent’. Use ‘ManualResetEvent.Wait’ inside your thread to decide if the operations can be continued. Use ‘ManualResetEvent.Set’ and ‘ManualResetEvent.Reset’ in your main thread.


Thursday, August 30, 2012 9:01 AM

ManualResetEvent run =new ManualResetEvent(false);

startbutton_click()
{
       Thead ts=new ThreadStart(Startmethod);
            th=new Thread(ts);                   
            th.Start(); 
}

Pausebutton_Click(object sender, EventArgs e)
        {
                                    
            
                if (th.ThreadState == System.Threading.ThreadState.Suspended)
                {
                    try
                    {
                        run.Reset();
                        label3.Text = "The Current thread has resumed again";
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Exception message:" + ex.Message);
                    }
                }
                else
                {
                    
                    try
                    {
                       run.Set();

                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Exception occured:" + ex.Message);
                    }
                   
                }

               
        }

the pause button doesnot work at all. what went wrong?.how i can set resume and suspend with manulaResetEvent


Thursday, August 30, 2012 1:25 PM

If you stop using suspend/resume then you cannot use ThreadState.Suspended anymore. Also, you didn't show the code for Startmethod we don't know how you use the manual reset event.


Thursday, August 30, 2012 3:33 PM

The waiting thread waits for the ManualResetEvent to be set. Calling Set opens the gate. Calling Reset closes it.

While the thread waits, its state will be WaitSleepJoin, not Suspended.

void Pausebutton_Click(object sender, EventArgs e){    if (th.ThreadState == System.Threading.ThreadState.WaitSleepJoin)    {        run.Set();        label3.Text = "The Current thread has resumed again";    }    else    {        run.Reset();    }}

Friday, August 31, 2012 3:39 AM

StartButton_Click(object sender,EventArgs e)
{

ThreadStart ts=new ThreadStart(startprocess);
Thread th=new Thread(ts);
th.Start();
}

public void startprocess()
{
//lines of code for the startprocess method
run.WaitOne();
}

This is my start method to start the thread.After changing the thread state to Waitsleepjoin my code doesnot work.where i went wrong?


Friday, August 31, 2012 3:49 AM

WaitOne appears to be the last thing in that method, is that ever going to actually wait? Don't you have some sort of loop before it?


Friday, August 31, 2012 4:10 AM

actually i don't have any loop before the run.WaitOne().But i tried this way

StartButton_Click(object sender,EventArgs e)

{
 while(true)
{

//line of code to process

run.WaitOne();
}

}

still didn't work why?

I think its not waiting at run.waitOne();

here while processing the line of code,suppose i want to pause these lines of code by clicking the pause button.It should wait once again i click the same pause to resume same thread.But it doesnot wait at all.Any idea to achieve this?


Friday, August 31, 2012 5:02 AM

Sleep and Resume weren't replaced by anything else. They were deprecated because they were a source of problems and not actually a solution to any problem.

As for why it might not be stopping at run.WaitOne() depends on what else is the program doing. The thread won't wait there if run is set to Signaled.


Friday, August 31, 2012 9:04 AM

actually i don't have any loop before the run.WaitOne().But i tried this way

StartButton_Click(object sender,EventArgs e){ while(true){//line of code to processrun.WaitOne();}}

still didn't work why?

I think its not waiting at run.waitOne();

here while processing the line of code,suppose i want to pause these lines of code by clicking the pause button.It should wait once again i click the same pause to resume same thread.But it doesnot wait at all.Any idea to achieve this?

You don't start a new thread anymore?


Friday, August 31, 2012 10:09 AM

sorry it was my mistake.The actual process will do like this way.

ManualResetEvent run=new ManualResetEvent(false);

StartButton_Click(object sender,EventArgs e)
 {

    ThreadStart ts=new ThreadStart(startprocess);
    Thread th=new Thread(ts);
    th.Start();
 }

public void startprocess()
{


  while(true)
  {
    if(a[0]==true)
     {
       //lines of code for the startprocess method
     }
    if(a[1]==true)
     {
       //lines of code for the startprocess method
     }
     run.WaitOne();

  }

void Pausebutton_Click(object sender, EventArgs e)
 {
    if (th.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
     {
        run.Set();
        label3.Text = "The Current thread has resumed again";
     }
   else
     {
        run.Reset();
     }
}

This is what i did to pause and resume the current thread.


Friday, August 31, 2012 11:28 AM

There are a number of actions which could put the thread in a WaitSleepJoin state. Use a boolean to keep track of its state:

bool waiting = true;void Pausebutton_Click(object sender, EventArgs e) {    if (waiting)     {        run.Set();        label3.Text = "The Current thread has resumed again";        waiting = false;     }   else     {        run.Reset();        waiting = true;     }}

Monday, September 3, 2012 8:50 AM

How ever i tried it does not wait at all after the changes.current running does not wait nor resume.how can i do this? any other suggestion?


Monday, September 3, 2012 9:00 AM

Have you checked in debugger if run.WaitOne() is ever reached?


Monday, September 3, 2012 9:12 AM

May be ur right.It does not reach the run.waitOne() line of code.Now where i can set this line of code to wait thread when i click pause button.?


Monday, September 3, 2012 9:18 AM | 1 vote

As I said in previous posts, it all depends one what that code is doing. Normally if you have a loop that does something then that loop needs to do run.WaitOne at least once per iteration. From the code you have shown that appears to be your case but if run.WaitOne ie never reached it means you probably have something else in that loop that prevents this (other nested loops, a continue statement, other Wait like calls).