简体   繁体   中英

Allow user to click on a Button of the form when Application is in Pause/Sleep Mode in C# winform Application

I am working on a C# winform application which continuously navigates some links in a webbrowser control and process the data of that webpage. I need to provide two buttons on the form for Pause and Resume.

On click of button the whole application should get pause processing and after that on click of Resume button it should start again.

So to pause the application, on click of Pause button I made thread to sleep for infinite time by following code.

private void Pause_Click(object sender, EventArgs e)
{
  System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}

But this piece of code unable the user to click on Resume Button on form to resume the application. Also I am not getting a perfect piece of code to resume the application on click of Resume Button.

Can Anyone get me the correct solution for this issue ?

Thread.Sleep method yields execution of code to process scheduler and doesn't get it back until specified time passes. Therefore you can't make sleeping thread wake itself up. You can't even make a working thread wake another sleeping thread (to my knowledge).

You need to accomplish your goal differently.

  • Separate data processing code to separate, worker, method
  • Run worker method in separate thread
  • Share a state variable between UI and worker thread. This variable should pass signals on whether execution should be paused.
  • From UI thread, set pause signals in shared variable as needed
  • In processing loop, write handler code for stopping processing

I'll post some pseudo code below on how you should do this:

private bool _Paused = false;
private void OnPauseClick()
{
    _Paused = true;
}
private void OnResumeClick()
{
    _Paused = false;
}
private void OnRunClick()
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerMethod));
}

private void WorkerMethod(object state)
{
    ...
    while (needToDoMoreWork)
    {
        // do some work here
        ...
        // if pause requested, wait to unpause
        while (_Paused)
        {
            // Sleep for a short time and check again if paused
            Thread.Sleep(100);
        }
    }
}

You'll need to fill in the blanks according to your business needs.

If you put your UI thread to indefinite sleep, you're going to kill your UI indefinitely. Don't do that.

Your application has some mechanism to know it's time to do its navigation and processing, probably a timer of some sort. If it's a timer, just stop it when you pause the application, and start it again when you resume. If it's some other mechanism, you need to stop it,too, but it's hard to tell you how without knowing what that mechanism is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM