Skip to content

Instantly share code, notes, and snippets.

@first087
Last active November 20, 2015 17:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save first087/9888796 to your computer and use it in GitHub Desktop.
Save first087/9888796 to your computer and use it in GitHub Desktop.
BackgroundWorkerEx on VB.NET
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmBGWorker
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.BtnStart = New System.Windows.Forms.Button
Me.BtnStop = New System.Windows.Forms.Button
Me.PgbDownloadPercent = New System.Windows.Forms.ProgressBar
Me.LblDownloadPercent = New System.Windows.Forms.Label
Me.LblDateTime = New System.Windows.Forms.Label
Me.TmrNow = New System.Windows.Forms.Timer(Me.components)
Me.BgwDownload = New System.ComponentModel.BackgroundWorker
Me.SuspendLayout()
'
'BtnStart
'
Me.BtnStart.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.BtnStart.Location = New System.Drawing.Point(68, 66)
Me.BtnStart.Name = "BtnStart"
Me.BtnStart.Size = New System.Drawing.Size(75, 23)
Me.BtnStart.TabIndex = 0
Me.BtnStart.Text = "Start"
Me.BtnStart.UseVisualStyleBackColor = True
'
'BtnStop
'
Me.BtnStop.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.BtnStop.Enabled = False
Me.BtnStop.Location = New System.Drawing.Point(149, 66)
Me.BtnStop.Name = "BtnStop"
Me.BtnStop.Size = New System.Drawing.Size(75, 23)
Me.BtnStop.TabIndex = 1
Me.BtnStop.Text = "Stop"
Me.BtnStop.UseVisualStyleBackColor = True
'
'PgbDownloadPercent
'
Me.PgbDownloadPercent.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.PgbDownloadPercent.Location = New System.Drawing.Point(12, 12)
Me.PgbDownloadPercent.Name = "PgbDownloadPercent"
Me.PgbDownloadPercent.Size = New System.Drawing.Size(268, 23)
Me.PgbDownloadPercent.TabIndex = 2
'
'LblDownloadPercent
'
Me.LblDownloadPercent.AutoSize = True
Me.LblDownloadPercent.Location = New System.Drawing.Point(12, 38)
Me.LblDownloadPercent.Name = "LblDownloadPercent"
Me.LblDownloadPercent.Size = New System.Drawing.Size(106, 13)
Me.LblDownloadPercent.TabIndex = 3
Me.LblDownloadPercent.Text = "LblDownloadPercent"
'
'LblDateTime
'
Me.LblDateTime.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.LblDateTime.AutoSize = True
Me.LblDateTime.Location = New System.Drawing.Point(12, 92)
Me.LblDateTime.Name = "LblDateTime"
Me.LblDateTime.Size = New System.Drawing.Size(67, 13)
Me.LblDateTime.TabIndex = 4
Me.LblDateTime.Text = "LblDateTime"
'
'TmrNow
'
Me.TmrNow.Enabled = True
'
'BgwDownload
'
Me.BgwDownload.WorkerReportsProgress = True
Me.BgwDownload.WorkerSupportsCancellation = True
'
'FrmBGWorkerTest
'
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None
Me.ClientSize = New System.Drawing.Size(292, 114)
Me.Controls.Add(Me.LblDateTime)
Me.Controls.Add(Me.LblDownloadPercent)
Me.Controls.Add(Me.PgbDownloadPercent)
Me.Controls.Add(Me.BtnStop)
Me.Controls.Add(Me.BtnStart)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "FrmBGWorkerTest"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "BackgroundWorker Test"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents BtnStart As System.Windows.Forms.Button
Friend WithEvents BtnStop As System.Windows.Forms.Button
Friend WithEvents PgbDownloadPercent As System.Windows.Forms.ProgressBar
Friend WithEvents LblDownloadPercent As System.Windows.Forms.Label
Friend WithEvents LblDateTime As System.Windows.Forms.Label
Friend WithEvents TmrNow As System.Windows.Forms.Timer
Friend WithEvents BgwDownload As System.ComponentModel.BackgroundWorker
End Class
Public Class frmBGWorker
' Synchronous version
#Region "Field"
Private _isBusy As Boolean
#End Region
#Region "Constructor & Destructor"
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
UpdateProgress(0)
LblDateTime.Text = ""
End Sub
#End Region
#Region "Local Function"
Private Sub UpdateProgress(ByVal percent As Integer)
PgbDownloadPercent.Value = percent
LblDownloadPercent.Text = String.Format("Download {0}%", percent)
End Sub
Private Sub DownloadStart()
_isBusy = True
For i As Integer = 1 To 100
If Not _isBusy Then Return
Threading.Thread.Sleep(50) ' Simulate download
UpdateProgress(i)
Next
_isBusy = False
End Sub
Private Sub DownloadStop()
_isBusy = False
End Sub
#End Region
#Region "Event"
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
BtnStop.Enabled = True
BtnStart.Enabled = False
DownloadStart()
BtnStart.Enabled = True
BtnStop.Enabled = False
MessageBox.Show("Download Complete")
End Sub
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
DownloadStop()
BtnStart.Enabled = True
BtnStop.Enabled = False
MessageBox.Show("Download Cancel")
End Sub
Private Sub TmrNow_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrNow.Tick
LblDateTime.Text = String.Format("{0} [Busy = {1}]", Now.ToString(), _isBusy)
End Sub
#End Region
End Class
Public Class frmBGWorker
' Asynchronous version
#Region "Constructor & Destructor"
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
UpdateProgress(0)
LblDateTime.Text = ""
End Sub
#End Region
#Region "Local Function"
Private Sub UpdateProgress(ByVal percent As Integer)
PgbDownloadPercent.Value = percent
LblDownloadPercent.Text = String.Format("Download {0}%", percent)
End Sub
#End Region
#Region "Event"
Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
BtnStop.Enabled = True
BtnStart.Enabled = False
' ตัวอย่าง RunWorkerAsync แบบไม่มี Input
BgwDownload.RunWorkerAsync()
' ตัวอย่าง RunWorkerAsync แบบมี Input
'Dim intStart As Integer = 1
'BgwDownload.RunWorkerAsync(intStart)
End Sub
Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
BgwDownload.CancelAsync()
End Sub
Private Sub TmrNow_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrNow.Tick
LblDateTime.Text = String.Format("{0} [Busy = {1}]", Now.ToString(), BgwDownload.IsBusy)
End Sub
Private Sub BgwDownload_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BgwDownload.DoWork
Dim intStart As Integer = 1
If e.Argument IsNot Nothing Then intStart = DirectCast(e.Argument, Integer)
For i As Integer = intStart To 100
If BgwDownload.CancellationPending Then e.Cancel = True : Return
Threading.Thread.Sleep(50) ' Simulate download
BgwDownload.ReportProgress(i)
' ตัวอย่างการทำให้เกิด Error เพื่อทดสอบการตรวจสอบ Error บน BackgroundWorker
'If i = 10 Then Throw New Exception("ทดสอบ error")
Next
e.Result = "ทดสอบ return ข้อมูล"
End Sub
Private Sub BgwDownload_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BgwDownload.ProgressChanged
UpdateProgress(e.ProgressPercentage)
End Sub
Private Sub BgwDownload_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BgwDownload.RunWorkerCompleted
BtnStart.Enabled = True
BtnStop.Enabled = False
Select Case True
Case e.Cancelled
MessageBox.Show("Download Cancel")
Case e.Error IsNot Nothing
MessageBox.Show("Download Error / Error Message = " & e.Error.Message)
Case Else
MessageBox.Show("Download Complete / Result = " & DirectCast(e.Result, String))
End Select
End Sub
#End Region
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment