|
■No86599 (モヒンダー さん) に返信
UIイベントでの待ちはよろしくないです。
フラグだけ立てて一旦イベントは終了するのがよいかと思います。
サンプルです。
Imports System.ComponentModel
Public Class Form1
Private flg1 As FLG
Private flg2 As FLG
Private flg3 As FLG
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
flg1 = New FLG With {.Value = True}
flg2 = New FLG With {.Value = True}
flg3 = New FLG With {.Value = True}
Label1.Text = "start"
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim isend = False
Do While Not isend
Dim f1 = False
Dim f3 = False
SyncLock flg1
f1 = flg1.Value
End SyncLock
SyncLock flg3
f3 = flg3.Value
End SyncLock
If Not f1 OrElse Not f3 Then
Exit Do
End If
Threading.Thread.Sleep(100)
Loop
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label1.Text = "end"
flg2.Value = False
If Not flg1.Value Then
Me.Close()
End If
End Sub
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
If flg2.Value Then
e.Cancel = True
SyncLock flg1
flg1.Value = False
End SyncLock
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SyncLock flg3
flg3.Value = False
End SyncLock
End Sub
End Class
Public Class FLG
Public Property Value As Boolean
End Class
|