C#BackgroundWorker未报告进度

时间:2014-05-13 04:23:41

标签: c# backgroundworker

我的应用中的后台工作程序不报告进度。 (设置断点,但永远不会达到)。我使用的代码如下所示。我已经在这个工作了几个小时,我很难过。通常的嫌疑人' (为WorkerReportsProgress设置布尔值,并将事件链接到_ProgressChanged,忘记调用.RunWorkerAsyn()已被检查)我很困惑为什么我的断点永远不会到达。

public partial class Form1 : Form
{
    BackgroundWorker bw_copyToTemp;  //Defined as a member of the class

    private void btnCopyToTemp_Click(object sender, EventArgs e)
    {

       bw_copyToTemp = new System.ComponentModel.BackgroundWorker
       {
           WorkerReportsProgress = true,
           WorkerSupportsCancellation = true
       };
       bw_copyToTemp.DoWork += bw_copyToTemp_DoWork;
       bw_copyToTemp.ProgressChanged += bw_copyToTemp_ProgressChanged;
       bw_copyToTemp.RunWorkerCompleted += bw_copyToTemp_RunWorkerCompleted;


       // Start background worker
       bw_copyToTemp.RunWorkerAsync();
    }

    void bw_copyToTemp_ProgressChanged(object sender,System.ComponentModel.ProgressChangedEventArgs e)
    {
        CurrentState s = (CurrentState)e.UserState;  //<<BREAKPOINT HERE NOT HIT
        toolStripStatusLabel1.Text = s.ProcessStep;
        toolStripProgressBar1.Value = e.ProgressPercentage;
    }

    void bw_copyToTemp_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {

        Properties.Settings prop = Properties.Settings.Default;
        BackgroundWorker worker = sender as BackgroundWorker;
        if (worker.WorkerReportsProgress)
            // Initialize the object that the background worker calls.
            worker.ReportProgress(0, new CurrentState("Called from within form"));  //<<< THIS LINE IS PROCESSED SINGLE STEPPING THROUGH CODE (NO CHANGE IN GUI)

        copyFileOnDVDToTemp.CopyOpticalToHardDrive(worker, e);


    }

    void bw_copyToTemp_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        // test code goes here
    }
}

&#34;当前状态&#34; class位于具有相同名称空间的不同.cs文件中:

   public class CurrentState
        {
            public CurrentState() { }
            public CurrentState(string processStep)
            {
                this.ProcessStep = processStep;
            }

            public string ProcessStep { get; set; }

        }

1 个答案:

答案 0 :(得分:3)

您错过了在按钮的Click事件处理程序中调用BackgroundWorker.RunWorkerAsync()方法。初始化后,您的工作人员无法启动。

private void btnCopyToTemp_Click(object sender, EventArgs e)
{

   bw_copyToTemp = new System.ComponentModel.BackgroundWorker
   {
       WorkerReportsProgress = true,
       WorkerSupportsCancellation = true
   };
   bw_copyToTemp.DoWork += bw_copyToTemp_DoWork;
   bw_copyToTemp.ProgressChanged += bw_copyToTemp_ProgressChanged;
   bw_copyToTemp.RunWorkerCompleted += bw_copyToTemp_RunWorkerCompleted;

   // Start background worker
   bw_copyToTemp.RunWorkerAsync();
}

请尝试以下操作,看看ReportProgress正在运行时调用DoWork

    void bw_copyToTemp_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
    {
        // Set breakpoint here
        CurrentState s = (CurrentState)e.UserState; 
    }

    void bw_copyToTemp_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 0; i <= 100; i++)
        {
            // Set breakpoint here
            worker.ReportProgress(i, new CurrentState("Percentage"));
        }
    }

调试您的应用程序,看看断点是如何被击中的。