Public Class Form1 '監視処理の開始 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.BackgroundWorker1.WorkerSupportsCancellation = True Me.BackgroundWorker1.RunWorkerAsync() End Sub '非常停止ボタン Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.BackgroundWorker1.CancelAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork While True If Me.BackgroundWorker1.CancellationPending = True Then e.Cancel = True Exit While End If '監視処理など End While 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 MsgBox("非常停止") End If End Sub End Class