简体   繁体   English

WPF中带有BackgroundWorker的ProgressBar

[英]ProgressBar with BackgroundWorker in WPF

I have an WPF Application where one of the tasks in this application will be printing a telerik report which takes a little time. 我有一个WPF应用程序,该应用程序中的一项任务是打印需要一点时间的telerik报告。

I have solved the screen freeze by using BackgroundWorker, But I want showing printing process in ProgressBar, I have read some of examples but all of them talks about FOR loops and passes the integer number to the ProgressBar which is dose not work with my case. 我已经使用BackgroundWorker解决了屏幕冻结问题,但是我想在ProgressBar中显示打印过程,我已经阅读了一些示例,但是所有示例都讨论了FOR循环,并将整数传递给ProgressBar,这对我的情况不起作用。

How can I do that if this possible? 如果可能的话我该怎么办?

Here's my BackgroundWorker DoWork: 这是我的BackgroundWorker DoWork:

 void _printWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        _receiptReport = new Receipt(_invoice.InvoiceID, _invoice.ItemsinInvoice.Count);
        printerSettings = new System.Drawing.Printing.PrinterSettings();
        standardPrintController = new System.Drawing.Printing.StandardPrintController();
        reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
        reportProcessor.PrintController = standardPrintController;
        instanceReportSource = new Telerik.Reporting.InstanceReportSource();
        instanceReportSource.ReportDocument = _receiptReport;
        reportProcessor.PrintReport(instanceReportSource, printerSettings);
    }

Thanks in advance 提前致谢

When you define your BackgroundWorker , enable reporting progress: 定义BackgroundWorker ,启用报告进度:

worker.WorkerReportsProgress = true;
worker.ProgressChanged += _printWorker_ProgressChanged;

Add a ProgressBar to your window. 将一个ProgressBar添加到您的窗口。 Set the Maximum to however many "updates" you want to report: 将“ Maximum ”设置为要报告的许多“更新”:

<ProgressBar x:Name="ProgressBar1" Maximum="8" />

Then increase the Value of the ProgressBar via the BackgroundWorker.ReportProgress method, after each line of code in your DoWork event: 然后,在DoWork事件中的每一行代码之后,通过BackgroundWorker.ReportProgress方法增加ProgressBarValue

void _printWorker_DoWork(object sender, DoWorkEventArgs e)
{
    var worker = (BackgroundWorker)sender;

    worker.ReportProgress(0);
    _receiptReport = new Receipt(_invoice.InvoiceID, _invoice.ItemsinInvoice.Count);
    worker.ReportProgress(1);
    printerSettings = new System.Drawing.Printing.PrinterSettings();
    ...
    ...
    worker.ReportProgress(6);
    instanceReportSource.ReportDocument = _receiptReport;
    worker.ReportProgress(7);
    reportProcessor.PrintReport(instanceReportSource, printerSettings);
    worker.ReportProgress(8);
}

private void _printWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    ProgressBar1.Value = e.ProgressPercentage;
}

You'll also need to make the ProgressBar visible prior to calling RunWorkerAsync() , then hide it in the RunWorkerCompleted event. 您还需要在调用RunWorkerAsync()之前使ProgressBar可见,然后将其隐藏在RunWorkerCompleted事件中。

You have to have an event handler for the ProgressChanged event for the background worker. 您必须具有后台工作程序的ProgressChanged事件的事件处理程序。 However, you need to have some number to pass to that event to indicate the percentage complete, or it's not very useful for updating a progress bar. 但是,您需要将一些数字传递给该事件以指示完成百分比,否则这对于更新进度条不是很有用。

My suggestion would be to just have an animated gif or something to indicate to the user that the application is currently working. 我的建议是仅使用动画gif或某些东西来向用户指示该应用程序当前正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM