Can someone spot the issue in this background thread implementation?
The docs say, the RunWorkerComple ted must fire irrespective of
exception. It does so on my machine when there is no loop. When the
loop is present, with at least a time delay, the completed event never
fires. Am I missing something?
Imports System.Threadin g
Public Class Form1
Dim _BackgroundWork erFinished As Boolean = False
Private WithEvents _BackgroundWork er As New
System.Componen tModel.Backgrou ndWorker
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
_BackgroundWork er.WorkerReport sProgress = True
_BackgroundWork er.RunWorkerAsy nc()
'uncommented, RunWorkerComple ted never fires on my machine.
'comment these 3 lines and RunWorkerComple ted fires as normal
'Do
' Threading.Threa d.Sleep(3000)
'Loop Until _BackgroundWork erFinished
TextBox1.Text += _BackgroundWork erFinished.ToSt ring
End Sub
Private Sub _BackgroundWork er_DoWork(ByVal sender As
System.Object, ByVal e As System.Componen tModel.DoWorkEv entArgs)
Handles _BackgroundWork er.DoWork
Throw New Exception("Blow up immediately.")
End Sub
Public Shared MonitorLock As Object = New Object()
Private Sub _BackgroundWork er_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As System.Componen tModel.RunWorke rCompletedEvent Args)
Handles _BackgroundWork er.RunWorkerCom pleted
If (e.Error IsNot Nothing) Then
MessageBox.Show (e.Error.Messag e)
End If
Monitor.Enter(M onitorLock)
_BackgroundWork erFinished = True
Monitor.Exit(Mo nitorLock)
TextBox1.Text = " end of run Completed "
End Sub
End Class
The docs say, the RunWorkerComple ted must fire irrespective of
exception. It does so on my machine when there is no loop. When the
loop is present, with at least a time delay, the completed event never
fires. Am I missing something?
Imports System.Threadin g
Public Class Form1
Dim _BackgroundWork erFinished As Boolean = False
Private WithEvents _BackgroundWork er As New
System.Componen tModel.Backgrou ndWorker
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
_BackgroundWork er.WorkerReport sProgress = True
_BackgroundWork er.RunWorkerAsy nc()
'uncommented, RunWorkerComple ted never fires on my machine.
'comment these 3 lines and RunWorkerComple ted fires as normal
'Do
' Threading.Threa d.Sleep(3000)
'Loop Until _BackgroundWork erFinished
TextBox1.Text += _BackgroundWork erFinished.ToSt ring
End Sub
Private Sub _BackgroundWork er_DoWork(ByVal sender As
System.Object, ByVal e As System.Componen tModel.DoWorkEv entArgs)
Handles _BackgroundWork er.DoWork
Throw New Exception("Blow up immediately.")
End Sub
Public Shared MonitorLock As Object = New Object()
Private Sub _BackgroundWork er_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As System.Componen tModel.RunWorke rCompletedEvent Args)
Handles _BackgroundWork er.RunWorkerCom pleted
If (e.Error IsNot Nothing) Then
MessageBox.Show (e.Error.Messag e)
End If
Monitor.Enter(M onitorLock)
_BackgroundWork erFinished = True
Monitor.Exit(Mo nitorLock)
TextBox1.Text = " end of run Completed "
End Sub
End Class
Comment