Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'To use these methods correctly you need to set both WorkerReportsProgress and WorkerSupportsCancellation to True
'Disable cancellation of the BackgroundWorker since it is not yet running
btnCancelBGWorker.Enabled = False
'Set the progress bar Maximum Value property to 100 - (just for show in this example)
ProgressBar1.Maximum = 100
'Start The timer on the UI Thread to pick up your Data every 5 Seconds (in theory - see comment below)
With Timer1
.Interval = 5000
.Enabled = True
End With
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Since we do not know how long the Database routine will take - lets turn the timer off so that we
'DO NOT have multiple Threads trying to update your Data at the same time. Once the BackgroundWorker
'has finished it's work we will then turn the Timer back on to continue to update every 5 seconds
Timer1.Enabled = False
'Reset the progress bar for the BackgroundWorker
ProgressBar1.Value = 0
'Now Enable cancellation of the BackgroundWorker since you may want to cancel it while it is running
btnCancelBGWorker.Enabled = True
'Now run the BackgroundWorker
BackgroundWorker1.RunWorkerAsync()
End Sub
'This method is executed in a worker thread when you call BackgroundWorker1.RunWorkerAsync
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'This is considerered best practice to allow handling of multiple background workers with the same method
Dim myWorker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
'Now lets do some time consuming work which would normally freeze the UI Thread
For i As Integer = 1 To 100
'Check to see if you have cancelled the BackgroundWorker process
If myWorker.CancellationPending Then
'The user has cancelled the background operation.
e.Cancel = True
Exit For
Else
'You can Raise the ProgressChanged event in the UI thread
'You can pass any object type to the userState property of the ReportProgress method which
'Therefore allows you to display any newly created objects in the current thread as
'the background worker is functioning - here we just pass a string back to the UI thread
myWorker.ReportProgress(i, i & " iterations complete")
'Perform some time-consuming operation here.
Threading.Thread.Sleep(100)
End If
Next i
End Sub
'This method is executed in the UI thread when you call myWorker.ReportProgress above
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
'Update the progress bar and display whatever you want
ProgressBar1.Value = e.ProgressPercentage
ListBox1.Items.Insert(0, e.UserState)
End Sub
'This method is executed in the UI thread when the BackgroundWorker has completed whatever it is doing
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
'Check to see if the BackgroundWorker Was cancelled and do something
If e.Cancelled Then
'The background operation was cancelled.
ListBox1.Items.Insert(0, "Operation Cancelled")
Else
ListBox1.Items.Insert(0, "Operation Completed")
'Start the Timer again and Disable the Cancel Button so you can continue doing your work every 5 seconds
btnCancelBGWorker.Enabled = False
Timer1.Enabled = True
End If
End Sub
Private Sub btnCancelBGWorker_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelBGWorker.Click
'If you cancel the BackgroundWorker then check to see if it is busy and Cancel it
If BackgroundWorker1.IsBusy Then
BackgroundWorker1.CancelAsync()
End If
End Sub
End Class