VB.net - Progressbar using BackgroundWorker
<p>I am trying to implement a progressbar using BackgroundWorker.</p>

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

<p>Below is my code in form:</p>

<pre><code>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
</code></pre>

<p>And below is my code in for which contains progressbar:</p>

<pre><code>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 &lt;&gt; _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
</code></pre>

<blockquote>
  <p>I have uploaded my project here:
  <a href="https://drive.google.com/file/d/0B7gzonuQsNbvaDZvR3ltSl9WNTg/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B7gzonuQsNbvaDZvR3ltSl9WNTg/view?usp=sharing</a></p>
</blockquote>