Skip to content

Instantly share code, notes, and snippets.

@dotcomboom
Created August 27, 2019 23:30
Show Gist options
  • Save dotcomboom/85664d17397574c1ebbdbe97f022a1f8 to your computer and use it in GitHub Desktop.
Save dotcomboom/85664d17397574c1ebbdbe97f022a1f8 to your computer and use it in GitHub Desktop.
Background Worker
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i = 0
While (i < 10 And BackgroundWorker1.CancellationPending = False)
i += 1
BackgroundWorker1.ReportProgress(i * 10)
My.Computer.Network.Ping("google.com")
End While
If BackgroundWorker1.CancellationPending = True Then
e.Cancel = True
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = e.ProgressPercentage & "%"
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled = True Then
Label1.Text = "Cancelled"
Else
Label1.Text = "Done!"
End If
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment