0

我是 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 个按钮,按钮 1 暂停,按钮 2 恢复。由于我找不到任何恢复的方法,所以我必须CancelAsync()在按下暂停时使用函数,并将进度条的值保存在全局变量中。然后,当我按下 resume 时,我再次使用RunWorkerAsync. 但问题是,我不能在这个函数中发送全局变量的值,所以它从 0 进度开始。

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

请给我一些解决方案。

4

1 回答 1

0

您可以尝试拥有自己的变量,即

bool isPaused = false;

当有人点击你的暂停按钮...

isPaused = true;

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

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

让我知道这对你有什么影响。

于 2013-03-16T18:25:46.643 回答