后台工作人员已完成事件在实际完成工作之前触发

时间:2015-07-24 21:03:15

标签: c# events backgroundworker

在我的应用程序中,当用户单击菜单项时,系统会提示他们选择一个文件然后使用后台工作程序加载和解析在此操作期间报告给进度条的进度。当工作完成后,我启动一个新窗口,我将解析后的数据传递给。由于某种原因,新窗口在解析数据之前被打开,并且传递了空参数而不是数据。

我的Click事件:

private void CIRCUIT_INSTALATION_SCHEDULED_Click(object sender, RoutedEventArgs e)
{
    //prevents users from selecting other menu items 
    disableAll();

    System.Windows.Forms.OpenFileDialog mainExcelFileDialog = new System.Windows.Forms.OpenFileDialog();
    mainExcelFileDialog.InitialDirectory = System.Configuration.ConfigurationManager.AppSettings["MainWorkbookDirectory"];
    mainExcelFileDialog.Title = "Main Excel File";
    mainExcelFileDialog.ShowDialog();
    string mainPath = mainExcelFileDialog.FileName;
    this.Cursor = System.Windows.Input.Cursors.Wait;
    BackgroundWorker cisWorker = new BackgroundWorker();

    cisWorker.DoWork += cisWorker_DoWork;
    cisWorker.ProgressChanged+=cisWorker_ProgressChanged;
    cisWorker.RunWorkerCompleted += cisWorker_RunWorkerCompleted;            
    cisWorker.RunWorkerAsync(mainPath);            
}

我的工作:

void cisWorker_DoWork(object sender, DoWorkEventArgs e)
{            
    BackgroundWorker worker = sender as BackgroundWorker;
    using (FileStream fs = new FileStream(e.Argument.ToString() , FileMode.Open))
    {
        //copy filestream to memorystream chunk by chunk, reporting progress.
        using (MemoryStream ms = new MemoryStream())
        {
            byte[] buffer = new byte[32768];
            int read;
            int steps = Convert.ToInt32(fs.Length / buffer.Length);
            int i = 0;
            while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
            {

                ms.Write(buffer, 0, read);
                i++;
                var progress = new Decimal(i) / new Decimal(steps);
                var outOf = Math.Round(progress * 50);
                worker.ReportProgress(Convert.ToInt32(outOf));
            }

            worker.ReportProgress(50);
            ms.Position = 0;
            //load excel workbook dataset from memorystream
            using (var xlr = Excel.ExcelReaderFactory.CreateOpenXmlReader(ms))
            {
                xlr.IsFirstRowAsColumnNames = true;
                mainWorkbook = xlr.AsDataSet();
                worker.ReportProgress(100);

            }

        }
    }
}

我完成了:

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.Cursor = System.Windows.Input.Cursors.Arrow;
    CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
    CIS_Window.Show();
    CIS_Window.Closed += CIS_Window_Closed;
}

编辑: 我将mainPath参数添加到cisWorker.runWorkerAsync()方法中。不幸的是,这个问题仍然存在。

2 个答案:

答案 0 :(得分:1)

基本上你应该检查你的数据是否为空,即在DoWork期间没有发生错误

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error == null)
    {
        /* Your mainWorkbook shouldn't be null */

        this.Cursor = System.Windows.Input.Cursors.Arrow;
        CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
        CIS_Window.Show();
        CIS_Window.Closed += CIS_Window_Closed;
    }
    else
    {
        // Handle error
    }
} 

答案 1 :(得分:0)

Henk Holterman是对的。我收到错误是因为我没有设置cisWorker.WorkerReportsProgress = true;我仍然不知道为什么错误没有打破调试器,因为它没有被处理,但我做了一个打印声明在我完成的方法中帮助我追查罪魁祸首。

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine(e.Error);
        this.Cursor = System.Windows.Input.Cursors.Arrow;
        CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow(mainWorkbook, abfsDatabase);
        CIS_Window.Show();
        CIS_Window.Closed += CIS_Window_Closed;
    }