Somebody
Somebody

Reputation: 2779

Pause and Resume a Thread

I have this code to pause and resume a thread:

    public partial class frmMain : Form
    {
        (...)        
        ManualResetEvent wait_handle = new ManualResetEvent(true);
        (...)
    }   

private void frmMain_Shown(object sender, EventArgs e)
    {                       
            ThreadPool.QueueUserWorkItem(new WaitCallback(TheLoop));
    }

private void TheLoop(object stateinfo)
        {
            bool hasInfo = true;
            while (doLoop)
            {
                wait_handle.WaitOne();

                bool hasLines = GetInfo();
                if (hasLines)
                {   
                    //Consuming time Operation 1
                    System.Threading.Thread.Sleep(7000);
                    if (CurrentLine < line.Count - 1)
                        CurrentLine++;
                    else
                    {                                                
                        bool hasInfo2 = GetInfo2();
                        if (hasInfo2)
                        {                            
                            //Consuming time Operation 2
                            System.Threading.Thread.Sleep(7000);
                        }


                        CurrentLine = 0;
                    }
                }
                else
                    System.Threading.Thread.Sleep(40000);  //Wait to query again
            }
        }

private void btnPauseResume_Click(object sender, EventArgs e)
        {
            if (btnPauseResume.Text == "Pause")
            {
                btnPauseResume.Text = "Resume";
                wait_handle.Reset();
            }
            else
            {
                btnPauseResume.Text = "Pause";
                wait_handle.Set();
            }
        }

The code above shows a cycle information, it works find to pause and resume the "first consuming time operation" but doesn't work for the second one, if I press the button to pause the thread in the second consuming time operation, this one continues and when the first one appears again, then it pauses there.

What am I missing here? Thx

Upvotes: 2

Views: 1980

Answers (4)

Somebody
Somebody

Reputation: 2779

Got it guys! the thing is where you put the WaitOne(). For instance, if I have a While Loop (like my example) if I put the wait before it, no matter how many times I hit the pause button, it won't stop the thread, it's logic since the loop already began, but if I put it at the end, then it will work.

Appreciated your help.

Upvotes: 0

Brian Gideon
Brian Gideon

Reputation: 48949

There is not any reason that I can see that would prevent a second call to WaitOne from working if placed before the 2nd time consuming operation. Since you are using a ManualResetEvent the wait handle's state will persist until either Set or Reset is called. That means if you resume the thread by calling Set then both calls to WaitOne will pass through. Likewise, if you pause the thread by calling Reset then both calls to WaitOne will block. Of course, it will not be possible to predict where the worker thread will pause if there is more than one call to WaitOne.

Upvotes: 0

Bryan Crosby
Bryan Crosby

Reputation: 6554

Have you considered using a Background Worker instead since you are using WinForms? It would probably be easier than trying to 'Pause' a thread. You can check the CancellationPending property to see if a user has elected to cancel the operation. The link has a good sample to look at.

Upvotes: 1

Tono Nam
Tono Nam

Reputation: 36048

I have never seen someone pausing a thread. Create a delegate and event inside the class or method that you are executing on a separate threat. Execute that event whenever you wish to pause your thred.

Upvotes: 0

Related Questions