Background Thread Issue

V

vapordan

Can someone spot the issue in this background thread implementation?
The docs say, the RunWorkerCompleted 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.Threading
Public Class Form1
Dim _BackgroundWorkerFinished As Boolean = False
Private WithEvents _BackgroundWorker As New
System.ComponentModel.BackgroundWorker

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

_BackgroundWorker.WorkerReportsProgress = True
_BackgroundWorker.RunWorkerAsync()

'uncommented, RunWorkerCompleted never fires on my machine.
'comment these 3 lines and RunWorkerCompleted fires as normal

'Do
' Threading.Thread.Sleep(3000)
'Loop Until _BackgroundWorkerFinished

TextBox1.Text += _BackgroundWorkerFinished.ToString
End Sub

Private Sub _BackgroundWorker_DoWork(ByVal sender As
System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Handles _BackgroundWorker.DoWork
Throw New Exception("Blow up immediately.")
End Sub
Public Shared MonitorLock As Object = New Object()

Private Sub _BackgroundWorker_RunWorkerCompleted(ByVal sender As
Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Handles _BackgroundWorker.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
End If

Monitor.Enter(MonitorLock)
_BackgroundWorkerFinished = True
Monitor.Exit(MonitorLock)

TextBox1.Text = " end of run Completed "
End Sub
End Class
 
I

Ignacio Machin ( .NET/ C# MVP )

Can someone spot the issue in this background thread implementation?
The docs say, the RunWorkerCompleted 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.Threading
Public Class Form1
    Dim _BackgroundWorkerFinished As Boolean = False
    Private WithEvents _BackgroundWorker As New
System.ComponentModel.BackgroundWorker

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

        _BackgroundWorker.WorkerReportsProgress = True
        _BackgroundWorker.RunWorkerAsync()

        'uncommented, RunWorkerCompleted never fires on my machine.
        'comment these 3 lines and RunWorkerCompleted fires as normal

        'Do
        '    Threading.Thread.Sleep(3000)
        'Loop Until _BackgroundWorkerFinished

        TextBox1.Text += _BackgroundWorkerFinished.ToString
    End Sub

    Private Sub _BackgroundWorker_DoWork(ByVal sender As
System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Handles _BackgroundWorker.DoWork
        Throw New Exception("Blow up immediately.")
    End Sub
    Public Shared MonitorLock As Object = New Object()

    Private Sub _BackgroundWorker_RunWorkerCompleted(ByVal sender As
Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Handles _BackgroundWorker.RunWorkerCompleted
        If (e.Error IsNot Nothing) Then
            MessageBox.Show(e.Error.Message)
        End If

        Monitor.Enter(MonitorLock)
        _BackgroundWorkerFinished = True
        Monitor.Exit(MonitorLock)

        TextBox1.Text = " end of run Completed "
    End Sub
End Class

hi

This is a C# group, your code is in VB.NET , please post in the
appropiated group
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top