Tejas Tejas - 2 months ago 32
Vb.net Question

VB.net - Progressbar using BackgroundWorker

I am trying to implement a progressbar using BackgroundWorker.

But the progress bar hides itself after a second and it doesn't remain on top till end. Not sure why.

Below is my code in form:

Private Sub btnProgressBarPOC_Click(sender As Object, e As EventArgs) Handles btnProgressBarPOC.Click
BackgroundWorker = New BackgroundWorker()
BackgroundWorker.WorkerReportsProgress = True
autoResetEvent = New AutoResetEvent(False)
ProgressBar = New frmProgressBar(BackgroundWorker)
ProgressBar.ShowDialog()

autoResetEvent.WaitOne()
MsgBox("Main Done", vbInformation)
End Sub

Sub Processing() Handles BackgroundWorker.DoWork
BackgroundWorker.ReportProgress(33)
Threading.Thread.Sleep(5000)

BackgroundWorker.ReportProgress(66)
Threading.Thread.Sleep(5000)

BackgroundWorker.ReportProgress(100)

MsgBox("Background Done", vbInformation)
AutoResetEvent.Set()
End Sub


And below is my code in for which contains progressbar:

Imports System.ComponentModel

Public Class frmProgressBar

Private WithEvents _BGW As System.ComponentModel.BackgroundWorker
Private _TaskInProgress As String

Public WriteOnly Property TaskInProgress()
Set(value)
_TaskInProgress = value
_BGW.ReportProgress(1)
End Set
End Property

Public Sub New(ByVal BGW As System.ComponentModel.BackgroundWorker)
_BGW = BGW
InitializeComponent()
End Sub

Private Sub frmProgress_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
If Not IsNothing(_BGW) Then
_BGW.RunWorkerAsync()
End If
End Sub

Private Sub _BGW_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles _BGW.ProgressChanged
progressBar.Value = e.ProgressPercentage

If Me.Text <> _TaskInProgress Then
Me.Text = _TaskInProgress
End If
End Sub

Private Sub _BGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _BGW.RunWorkerCompleted
Me.Close()
_TaskInProgress = ""
End Sub

'Private Sub _BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles _BGW.DoWork
' Do While True
' ' Has the background worker be told to stop?
' If _BGW.CancellationPending Then
' ' Set Cancel to True
' e.Cancel = True
' Exit Do
' End If
' System.Threading.Thread.Sleep(2000) ' Sleep for 1 Second
' Loop
'End Sub
End Class



I have uploaded my project here:
https://drive.google.com/file/d/0B7gzonuQsNbvaDZvR3ltSl9WNTg/view?usp=sharing

Answer

Below is the corrected code which worked very well:

Public Class frmMisc
    Private WithEvents BackgroundWorker As BackgroundWorker, ProgressBar As frmProgressBar

    Private Sub btnProgressBarPOC_Click(sender As Object, e As EventArgs) Handles btnProgressBarPOC.Click
        Call RunProcessing()

        MsgBox("Main Done", vbInformation)
    End Sub

    Sub RunProcessing()
        BackgroundWorker = New BackgroundWorker()
        BackgroundWorker.WorkerReportsProgress = True
        ProgressBar = New frmProgressBar(BackgroundWorker)
        ProgressBar.ShowDialog()
    End Sub

    Sub Processing() Handles BackgroundWorker.DoWork
        BackgroundWorker.ReportProgress(33, "Step-1")
        Threading.Thread.Sleep(3000)

        BackgroundWorker.ReportProgress(66, "Step-2")
        Threading.Thread.Sleep(2000)

        BackgroundWorker.ReportProgress(100, "Step-3")
    End Sub
End Class

Public Class frmProgressBar

    Private WithEvents _BGW As System.ComponentModel.BackgroundWorker
    Private _TaskInProgress As String

    Public Sub New(ByVal BGW As System.ComponentModel.BackgroundWorker)
        _BGW = BGW
        InitializeComponent()
    End Sub

    Private Sub frmProgress_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        If Not IsNothing(_BGW) Then
            _BGW.RunWorkerAsync()
        End If
    End Sub

    Private Sub _BGW_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles _BGW.ProgressChanged
        progressBar.Value = e.ProgressPercentage

        Me.Text = e.UserState
    End Sub

    Private Sub _BGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _BGW.RunWorkerCompleted
        Me.Close()
        _TaskInProgress = ""
    End Sub
End Class

I was doing up Me.Text outside of ProgressChanged even which was causing this issue. Thanks to @Hans for providing hint about this!