views:

3358

answers:

2

In my code, when a button is clicked the progress bar is set to marquee and then my BackgroundWorker is called but when the BackgroundWorker is called the progress bar freezes or disappears. I use the BackgroundWorker to seperate the RefreshReport method of the ReportViewer from the UI thread. Any help is appreciated. Thanks!

    Private Sub btnOtherReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOtherReport.Click
        rvReport.ProcessingMode = ProcessingMode.Remote
        rvReport.ShowParameterPrompts = False
        rvReport.ServerReport.ReportServerUrl = New Uri("REPORT_SERVER_URL")
        rvReport.ServerReport.ReportPath = "REPORT_PATH"
        rvReport.BackColor = Color.White

        BackgroundWorker1.RunWorkerAsync()
    End Sub


    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        RefreshReport()
    End Sub


    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        pbReports.Style = ProgressBarStyle.Blocks
        pbReports.Value = 100
    End Sub


    Public Sub RefreshReport()
        rvReport.BeginInvoke(New MethodInvoker(AddressOf rvReport.RefreshReport))
    End Sub
A: 

The problem is that the progress bar is not being given a chance to repaint while the background thead has the processor. So you need to call System.Windows.Forms.Application.DoEvents() from the main waiting thread while you are processing. This will get control and cause the progress bar to repaint.

So after the BackgroundWorker1.RunWorkerAsync call you can add a loop to call DoEvents until an event is raised and then raise the event in RunWorkerCompleted to let the calling code exit. Since this doesnt allow the main code to continue running you could put the call in the Background call or another thread.

Marcus Erickson
+2  A: 

The problem is when you call .BeginInvoke() in your RefreshReport() method. The BackgroundWorker.DoWork() method is already raised in a different thread, so you can just call rvReport.RefreshReport(). It should look like this:

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    rvReport.RefreshReport()
End Sub

It really is that simple, with the possible addition of using a Monitor to lock your report object and prevent re-entry.

Right now, when you call .BeginInvoke() the report process kicks off, but it doesn't block at all so there's nothing left for the DoWork() method to do. It just returns right away. At this point the BackgroundWorker thinks it's done, so calls the .RunWorkerCompleted() method, which stops your progress bar.


Based on the comment, rvReport is a visual control rather than a component or simple data access class. In that case, you should know that visual controls in .Net are not thread safe, and therefore should never directly do anything directly that takes more than a few moments to complete. The hoops you were jumping through with .BeginInvoke() in the RefreshReport() method had the effect of calling your long running function in the main UI thread.

To solve this problem, you need to either turn off cross thread checking so that the exception is not thrown (simple, but not recommended) or change how you use the control, so that the main work happens elsewhere and the control just raises events when things are ready. If you can't modify the control to that extent, it's a design flaw in the control.

Joel Coehoorn
When I call rvReport.RefreshReport() from the DoWork() method, I get an exception:Cross-thread operation not valid: Control 'winRSviewer' accessed from a thread other than the thread it was created on.
Bryan Roth
I guess it comes down to the fact that when I call the RefreshReport() method of the ReportViewer, it takes a long time to generate the report and makes the form unresponsive. I have done testing and it has nothing to do with the report but is in fact a problem with the ReportViewer.
Bryan Roth