Question

I am developing a window application in VB.net and i as using backgroundworker. Maybe it is a very simple question, but is it possible report the progress as a double number and not as the integer part of the progress percentage?

I need the full number in order to display some more info, and i can only do this when i know the exact iteration the algorithm is in.

Is there any easy way doing so?

Thanks in advance!

Was it helpful?

Solution

The ReportProgress method has two overloads. The first one takes only a percentProgress As Integer parameter, but the second one takes an additional userState As Object parameter. With that second overload, you can pass any type of data that you want. In your case, you could pass a Double value as your user-state, like this

BackgroundWorker1.ReportProgress(0, myDouble)

Then, in the ProgressChanged event handler, you can convert the value back to a Double, like this:

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Dim myDouble As Double = CDbl(e.UserState)
    ' ...
End Sub

As in the above example, if you don't need the percentProgress parameter, you can just pass a value of 0 for that parameter. You are not limited to passing just one or two values either. If you need to pass additional information, such as a status string, you could do so by creating your own class to encapsulate all of the status-related data and then pass one of those objects as your userState parameter. For instance:

Public Class MyUserState
    Public Property MyDouble As Double
    Public Property StatusDescription As String
End Class

Then you could call the ReportProgress method like this:

Dim myState As New MyUserState()
myState.MyDouble = 1.1
myState.StatusDescription = "Test"
BackgroundWorker1.ReportProgress(0, myState)

Then, you can read the values like this:

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Dim myState As MyUserState = DirectCast(e.UserState, MyUserState)
    ' ...
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top