WPF ProgressBar - 在操作完成后使进度条完成

时间:2011-03-31 16:27:16

标签: c# wpf progress-bar backgroundworker system.diagnostics

我有一个WPF表单,它使用进度条运行后台操作。但问题是;

操作完成后,进度条仍在运行。我的意思是它表明操作正在进行中。

我怎么能阻止它?这是我的整个代码;

System.ComponentModel.BackgroundWorker mWorker;

private void button1_Click(object sender, RoutedEventArgs e) {

    mWorker = new System.ComponentModel.BackgroundWorker();
    mWorker.DoWork +=new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    mWorker.ProgressChanged +=new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
    mWorker.WorkerReportsProgress = true;
    mWorker.WorkerSupportsCancellation = true;
    mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    mWorker.RunWorkerAsync();

    while (pbProcessing.Value != 100) {

        if (!mWorker.CancellationPending) {

            try {

                pbProcessing.Value = (pbProcessing.Value + 0.01) % 100;

            } catch (System.Exception ex) {

                // No action required
            }

        } else {

            MessageBox.Show(this, "Process cancelled", "Cancel Process", MessageBoxButton.OK);
            break;

        }

        System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
               new System.Threading.ThreadStart(delegate { }));

    }

}

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

    // Do your work here, its on seperate thread
    System.Threading.Thread.Sleep(10000);

}
private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) {

    pbProcessing.Value = e.ProgressPercentage;

}
private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {

    // Stop Progressbar updatation  
    Window1 w = new Window1();
    w.Browser.Navigate(new Uri("http://stackoverflow.com"));
    w.Show();

}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {

    if (mWorker != null) {

        if (mWorker.IsBusy) {
            mWorker.CancelAsync();
        }

    }

}

1 个答案:

答案 0 :(得分:4)

如果要在完成工作后隐藏进度条,请将其Visibility属性设置为Visibility.Hidden。如果您只是想将其重置为初始状态,请将其Value设置为0(或pbProgressing.Minimum,如果您将其更改为默认值)。


作为旁注,你的代码没有意义:不是在按钮事件处理程序中连续更改pbProcessing.Value(这完全没用,因为在按钮事件处理程序完成之前不会执行UI更新),你应该只改变ProgressChanged中的值。即,您的代码应该如下所示:

System.ComponentModel.BackgroundWorker mWorker;

private void button1_Click(object sender, RoutedEventArgs e) {

    mWorker = new System.ComponentModel.BackgroundWorker();
    mWorker.DoWork +=new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    mWorker.ProgressChanged +=new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
    mWorker.WorkerReportsProgress = true;
    mWorker.WorkerSupportsCancellation = true;
    mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    mWorker.RunWorkerAsync();

    // Don't do anything else here

}

private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
    for (int i = 1; i < 100; i++) {
        mWorker.ReportProgress(i);

        // Do some part of the work
        System.Threading.Thread.Sleep(100);

        // Check if the user wants to abort
        if (mWorker.CancellationPending) {
            e.Cancel = true;
            return;
        }
    }

    mWorker.ReportProgress(100);  // Done
}

private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) {
    pbProcessing.Value = e.ProgressPercentage;
}

private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) {

    // Stop Progressbar updatation  
    Window1 w = new Window1();
    w.Browser.Navigate(new Uri("http://stackoverflow.com"));
    w.Show();

    // Check the result
    if (e.Cancelled) {
        // show the message box that the task has been canceled
    }

    // Reset Progress bar
    pbProcessing.Value = 0;
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
    if (mWorker != null) {
        if (mWorker.IsBusy) {
            mWorker.CancelAsync();
        }
    }
}