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
The problem is when you call
.BeginInvoke()in yourRefreshReport()method. TheBackgroundWorker.DoWork()method is already raised in a different thread, so you can just callrvReport.RefreshReport(). It should look like this:It really is that simple, with the possible addition of using a
Monitorto 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 theDoWork()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,
rvReportis 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 theRefreshReport()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.