I have a background process:
Public Shared Function CheckForInternetConnection() As Boolean
Try
Using client = New WebClient()
Using stream = client.OpenRead("http://www.google.com")
Return True
End Using
End Using
Catch
Return False
End Try
End Function
This process ties up my application, causing it to hang until completed.
I'm trying to incorporate this into a "BackGroundWorker" and tie it to a progress bar.
However, though the application no longer hangs when the process is running, the progress bar does.
I'd like the progress to progress from 0 - 50% during the above function, however currently, the progress bar sits at 0% until the process is complete, them jumps to 50%.
Private Sub bgwLongTask_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwLongTask.DoWork
For i As Integer = 1 To 10
' If we should stop, do so.
If (bgwLongTask.CancellationPending) Then
' Indicate that the task was canceled.
e.Cancel = True
Exit For
End If
CheckForInternetConnection()
LoadingScreen.CheckForIllegalCrossThreadCalls = False
Me.Text = i
' Notify the UI thread of our progress.
bgwLongTask.ReportProgress(i * 10)
Next i
End Sub
Private Sub bgwLongTask_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwLongTask.ProgressChanged
If e.ProgressPercentage >= 0% And e.ProgressPercentage <= 50% Then
lblStatus.Text = "Checking Netowrk ..."
Else
lblStatus.Text = "Loading ..."
End If
prgPercentComplete.Value = e.ProgressPercentage
End Sub
By using:
LoadingScreen.CheckForIllegalCrossThreadCalls = False
Me.Text = i
I can see that the progress bar doesn't even start until CheckForInternetConnection()
has completed.
Can someone please help me sort this out?
Much appreciated. Thanks.
You're looping from 1 to 10 calling CheckForInternetConnection()
in every iteration, that doesn't makes sense. In this case the long-running process is CheckForInternetConnection()
method, so you need to somehow "report progress" while the method is executing.
Possible way to notify user that the program is processing something when we can't calculate progress percentage of the process is using progress bar in indeterminate mode (by setting Style
to Marquee
in WinForm or setting IsIndeterminate
to True
in WPF).