2

我有一个需要很长时间的过程,我想要一个窗口来显示进度。但是,我不知道如何显示进度。

这是代码:

if (procced)
{
    // the wpf windows :
    myLectureFichierEnCour = new LectureFichierEnCour(_myTandemLTEclass);
    myLectureFichierEnCour.Show();

    bgw = new BackgroundWorker();
    bgw.DoWork += startThreadProcessDataFromFileAndPutInDataSet;
    bgw.RunWorkerCompleted += threadProcessDataFromFileAndPutInDataSetCompleted;

    bgw.RunWorkerAsync();
}

和:

private void startThreadProcessDataFromFileAndPutInDataSet(object sender, DoWorkEventArgs e)
{
    _myTandemLTEclass.processDataFromFileAndPutInDataSet(
        _strCompositeKey,_strHourToSecondConversion,_strDateField);
}

我可以打电话询问_myTandemLTEclass.processProgress进展情况。

4

2 回答 2

6

您应该在那里处理ProgressChanged事件并更新用户界面中的进度条。

在执行工作的实际函数(DoWork事件处理程序)中,您将使用指定已完成任务量的参数调用实例的ReportProgress方法。BackgroundWorker

MSDN 库中的BackgroundWorker 示例是完成这项工作的简单代码片段。

于 2009-12-03T21:26:54.927 回答
1

您的 backgroundWorker 线程需要处理DoWork方法和ProgressChanged.

您还需要确保将WorkerReportsProgress标志打开为真(默认情况下关闭)。

见示例代码:

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
于 2009-12-03T21:31:09.660 回答