Public Class Main
Dim Worker As BackgroundWork = New BackgroundWork
Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loa
Worker.Call_on_End = New BackgroundWork.Ended(AddressOf Ended)
Worker.Report_Prog = New BackgroundWork.Advance_Progress(AddressOf Set_Progress)
Worker.Start()
End Sub
Public Sub Set_Progress(ByVal index As Integer, ByVal max As Integer, ByVal Title As String)
ProgressBar.Maximum = max
ProgressBar.Value = index
If Title.ToString <> "" Then Me.Text = Title
'Here is where you put what is to be done on progress update.
End Sub
Public Sub Ended()
'Here is where you put what is to be done on process end.
End Sub
End Class
Public Class BackgroundWork
Public Sub Work(Optional ByVal Worker As System.ComponentModel.BackgroundWorker = Nothing, Optional ByVal e As System.ComponentModel.DoWorkEventArgs = Nothing, Optional ByVal oProgress As Object = Nothing)
For I As Integer = 1 To 100
'Here is where you put the work to be done.
System.Threading.Thread.Sleep(10)
Worker.ReportProgress(0, New Status(I, 100, ""))
Next
End Sub
Private WithEvents Worker As New System.ComponentModel.BackgroundWorker
Public Delegate Sub Advance_Progress(ByVal index As Integer, ByVal max As Integer, ByVal title As String)
Public Report_Prog As Advance_Progress
Public Delegate Sub Ended()
Public Call_on_End As Ended
Private Cancelled As Boolean = False
Sub Start()
Report_Prog(0, 0, "")
Worker.WorkerReportsProgress = True
Worker.WorkerSupportsCancellation = True
Worker.RunWorkerAsync()
End Sub
Private Sub Background_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker.ProgressChanged
Dim Progress As Status = CType(e.UserState, Status)
Report_Prog(Progress.Index, Progress.Max, Progress.Title)
End Sub
Private Sub Background_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
Dim Worker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Work(Worker, e)
End Sub
Public Sub Cancel()
Worker.CancelAsync()
Cancelled = True
Call_on_End()
End Sub
Private Class Status
Private _index As Integer = 0
Private _max As Integer = 0
Private _title As String = DBNull.Value.ToString
ReadOnly Property Index() As Integer
Get
Return _index
End Get
End Property
ReadOnly Property Max() As Integer
Get
Return _max
End Get
End Property
ReadOnly Property Title() As String
Get
Return _title
End Get
End Property
Sub New(ByVal Index As Integer, ByVal Max As Integer, Optional ByVal Title As String = "")
_index = Index
_max = Max
_title = Title
End Sub
End Class
End Class