如何实现暂停和恢复与BackgroundWorker的C#功能(How to implement P

2019-09-26 06:08发布

我想实现暂停和恢复用BackgroundWorker的取消功能。 我有一个用户控制和写在用户控制所有的BackgroundWorker相关的代码。 我只是加上流布局控制多达上的按钮时用户点击的用户控制。 我是不能暂停教学贯彻与 ManualResetEvent的 恢复功能,但无论如何,我没有能够做到这一点。 所以我在这里粘贴我的用户控件类相关的代码和我的表单代码。

public partial class ucBackgroundWorker : UserControl
{
    System.ComponentModel.BackgroundWorker bgWorker = null;
    public event Action<string, EventArgs> Done;
    public event Action<string, EventArgs> Cancel;
    private static bool m_continue = true;
    private ManualResetEvent _resetEvent = new ManualResetEvent(false);
    //Semaphore sWaiter = new Semaphore(0, 1);

    public ucBackgroundWorker()
    {
        InitializeComponent();
        bgWorker = new System.ComponentModel.BackgroundWorker();
        bgWorker.WorkerSupportsCancellation = true;
        bgWorker.WorkerReportsProgress = true;
        bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        bgWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

    public void Run(int counter)
    {
        if (!bgWorker.IsBusy)
        {
            bgWorker.RunWorkerAsync(counter);
        }
        _resetEvent.Set();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int input = int.Parse(e.Argument.ToString());

        this.BeginInvoke((MethodInvoker)delegate
        {
            lblStatus.Text = "Running";
        });

        for (int i = 1; i <= input; i++)
        {
            _resetEvent.WaitOne();
            Thread.Sleep(500);
            (sender as System.ComponentModel.BackgroundWorker).ReportProgress(i * 10);
            if ((sender as System.ComponentModel.BackgroundWorker).CancellationPending)
            {
                this.BeginInvoke((MethodInvoker)delegate
                {
                    lblStatus.Text = "Cancel";
                });

                e.Cancel = true;
                return;
            }
        }
        Thread.Sleep(1000);
    }


    // This event handler deals with the results of the 
    // background operation. 
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // First, handle the case where an exception was thrown. 
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message);
        }
        else if (e.Cancelled)
        {
            if (Cancel != null)
                Cancel(this.Name, EventArgs.Empty);
        }
        else
        {
            this.BeginInvoke((MethodInvoker)delegate
            {
                lblStatus.Text = "Done";
            });

            if (Done != null)
                Done(this.Name, EventArgs.Empty);
        }
        _resetEvent.Reset();
    }

    // This event handler updates the progress bar. 
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        pBar.Refresh();
        pBar.Value = e.ProgressPercentage;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        if (bgWorker.IsBusy)
        {
            bgWorker.CancelAsync();
        }
    }

    private void btnPause_Click(object sender, EventArgs e)
    {
        if (bgWorker.IsBusy)
        {
            if (btnPause.Text.ToUpper() == "PAUSE")
            {
                btnPause.Text = "Resume";
                m_continue = false;
                _resetEvent.Reset();
            }
            else if (btnPause.Text.ToUpper() == "RESUME")
            {

                btnPause.Text = "Pause";
                m_continue = true;
                _resetEvent.Set();

            }
        }
    }
}

    public partial class Form3 : Form
        {
            ucBackgroundWorker ucBgWorker = null;

            public Form3()
            {
                InitializeComponent();
            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                ucBgWorker = new ucBackgroundWorker();
                ucBgWorker.Done += new Action<string, EventArgs>(Worker_Done);
                ucBgWorker.Cancel += new Action<string, EventArgs>(Worker_Cancel);
                flowLayoutPanel1.Controls.Add(ucBgWorker);
                ucBgWorker.Run(10);
            }

            void Worker_Done(string arg, EventArgs evtarg)
            {
                label1.Text =arg + " Done One";
                //System.Threading.Thread.Sleep(1000);
            }

            void Worker_Cancel(string arg, EventArgs evtarg)
            {
                label1.Text = arg + " Cancel Click";
                //System.Threading.Thread.Sleep(1000);
            }

        }

在这里我附上亩UI的PIC它的外观。

Answer 1:

添加属性到您的类将举行布尔 - 睡眠。 订阅这种变化,如果这是真的集合了Thread.Sleep一些大的数字,然后订阅这个数字变成0,如果布尔仍然是它真正的设置为大数目一次。 这就是在我脑海中现在有肯定的更好的方法。



文章来源: How to implement Pause & Resume functionality with BackgroundWorker c#