Background Thread Issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vapordan@gmail.com

    Background Thread Issue

    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
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Background Thread Issue

    On Aug 1, 1:47 pm, vapor...@gmail. com wrote:
    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
    hi

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

    Comment

    • Peter Duniho

      #3
      Re: Background Thread Issue

      On Fri, 01 Aug 2008 10:47:24 -0700, <vapordan@gmail .comwrote:
      Can someone spot the issue in this background thread implementation?
      As Ignacio says, if you're writing VB code, you really ought to ask your
      question in a VB newsgroup.

      That said, the problem with your code is that you are negating the whole
      point of using BackgroundWorke r by not exiting your Button1_Click event
      handler once you've started the BackgroundWorke r.

      The RunWorkerComple ted event is raised on the GUI thread. Since you
      didn't ever exit your event handler, the GUI thread is blocked. This is
      always a bad thing anyway, and avoiding that is the main reason to be
      using BackgroundWorke r in the first place. But on top of that, since
      you've blocked the GUI thread, along with everything else that won't work
      is included the RunWorkerComple ted event.

      So, the fix is to remove the loop. Which you already knew, in a sort of
      way. :)

      Pete

      Comment

      Working...