Question

I have an WPF Application where one of the tasks in this application will be printing a telerik report which takes a little time.

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.

How can I do that if this possible?

Here's my 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

Was it helpful?

Solution

When you define your BackgroundWorker, enable reporting progress:

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

Add a ProgressBar to your window. Set the Maximum to however many "updates" you want to report:

<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:

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.

OTHER TIPS

You have to have an event handler for the ProgressChanged event for the background worker. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top