简体   繁体   English

当应用程序处于C#winform应用程序的暂停/睡眠模式时,允许用户单击表单的按钮

[英]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. 我正在使用一个C#winform应用程序,该应用程序连续浏览Web浏览器控件中的某些链接并处理该网页的数据。 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. Thread.Sleep方法使代码执行到进程调度程序,并且直到指定的时间过去才将其取回。 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. 在UI和辅助线程之间共享状态变量。 This variable should pass signals on whether execution should be paused. 此变量应传递有关是否应暂停执行的信号。
  • From UI thread, set pause signals in shared variable as needed 在UI线程中,根据需要在共享变量中设置暂停信号
  • 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. 如果您将UI线程无限期地睡眠,则将无限期地杀死UI。 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. 如果是其他机制,您也需要停止它,但是如果不知道该机制是什么,很难告诉您如何做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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