[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

BackgroundWorker.WorkerReportsProgress Fails

In trying to use the BackgroundWorker I have been unable to get it to report progress. The WorkerReportsProgress property is set to True but that does not seem to work. The thread is a subroutine in a separate module that is accessed from several locations so I don't want to copy it over for each location. The background thread works fine without progress reporting.  The code snippet shows what I think are the relevant items.
Code in calling form:
 
		'preliminary stuff of no interest
            BackgroundWorker2.WorkerSupportsCancellation = True
            BackgroundWorker2.RunWorkerAsync()
				Catch ex as exception
						'exception handling
				End Try
		End Sub
    Private Sub BackgroundWorker2_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        BackgroundWorker2.WorkerReportsProgress = True
        StratBuild()
 
    End Sub
 
 
Code in background thread:
 
					'preliminary stuff of no interest
 
            If IsFormLoaded("frmReadInvoiceFile") And AuditInfo.FileType > 4 Then
                If rs3.RecordCount > 0 Then
                     frmReadInvoiceFile.BackgroundWorker2.ReportProgress(Math.Min(100, 100 * RecNbr / rs3.RecordCount))
                End If
            End If
            
           'more stuff of no interest

Open in new window

0
rkulp
Asked:
rkulp
1 Solution
 
Bob LearnedCommented:
I can't see how "RecNbr" is incremented.

Bob
0
 
J1H1W1Commented:
You need to set the BackgroundWorker.ReportsProgess property before you call RunWorkerAsync.  Then subscribe to the ProgressChanged event.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rkulpAuthor Commented:
Bob,

RecNbr is incremented in prior code. I don't think it has a bearing on this error.

J1H1W1,

In the original way I had the code, BackgroundWorker1.WorkerReportsProgress = True was before the call to RunWorkerAsync().

Eric,

I'll look at it. Thanks for the link.
0
 
rkulpAuthor Commented:
Eric,
None of my references used the code:
Dim worker as BackgroundWorker = CType(sender,BackgroundWorker).
The code I had required the New sub to initialize the form which was referenced and that caused the problem.  Thanks very much.
0
 
rkulpAuthor Commented:
Eric's example provided the clue. Putting the BackgroundWorker and DoWorkEventArgs in as parameters to the subroutine and then referencing them in the subroutine solved the problem. The real problem was in the full reference:  
frmReadInvoiceFile.BackgroundWorker2.ReportProgress(Math.Min(100, 100 * RecNbr / rs3.RecordCount))
This required the form to be initialized again and the BackgroundWorker properties were lost. His example provided a complete solution.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now