在此期间恢复线程并处理另一个按钮

时间:2013-03-16 18:18:11

标签: c# multithreading

我是C#的新手。我找到了一些适用于进度条的代码。做什么,当有人点击按钮开始btnStartAsyncOperation_Click时,进度条开始增加,当按下btnCancel_Click时取消操作。这是代码

namespace BackgroundWorkerSample
{
    public partial class Form1 : Form
    {       
        BackgroundWorker m_oWorker;

        public Form1()
        {
            InitializeComponent();           
            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(abcd);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
        }

        void abcd(object sender, RunWorkerCompletedEventArgs e)
        {           
            if (e.Cancelled)
            {
                lblStatus.Text = "Task Cancelled.";
            }
            else if (e.Error != null)
            {
                lblStatus.Text = "Error while performing background operation.";
            }
            else
            {
                lblStatus.Text = "Task Completed...";
            }
            btnStartAsyncOperation.Enabled = true;
            btnCancel.Enabled = false;
        }

        void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }

        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                m_oWorker.ReportProgress(i);
                if (m_oWorker.CancellationPending)
                {
                    e.Cancel = true;
                    m_oWorker.ReportProgress(0);
                    return;

                }

            }

            //Report 100% completion on operation completed
            m_oWorker.ReportProgress(100);
        }

        private void btnStartAsyncOperation_Click(object sender, EventArgs e)
        {
            btnStartAsyncOperation.Enabled  = false;
            btnCancel.Enabled               = true;
            //Start the async operation here

            m_oWorker.RunWorkerAsync();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

            if (m_oWorker.IsBusy)
            {
                //Stop/Cancel the async operation here
                m_oWorker.CancelAsync();
            }
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void lblStatus_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            }

        private void button2_Click(object sender, EventArgs e)
        {

        }
        }
    }

现在我又添加了2个按钮,button1暂停,button2恢复。由于我找不到任何方法可以恢复,因此当我按下暂停时我不得不使用CancelAsync()函数,并将进度条的值保存在全局变量中。然后,当我按下继续时,我使用RunWorkerAsync再次启动进度条。但问题是,我无法在此函数中发送全局变量的值,因此它从0开始。

当有人按下暂停时我尝试使用thread.sleep(infinite time here),然后当有人按下简历时停止线程。问题仍然是,在这种情况下我不能按任何按钮。如果我启用按钮,它们仍然不起作用。

请给我一些解决方案。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用自己的变量,即

bool isPaused = false;

当有人点击暂停按钮时...

isPaused = true;

当有人点击恢复时将其设置为false。最后,在doWork方法的for循环中,让它等到该变量为false。

while (isPaused) 
{
    Thread.Sleep(100);
}

让我知道这对你有用。