Multithreading Race Conditions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69

    #1

    Multithreading Race Conditions

    Im having an issue setting back my databackgroundw orker.is busy to
    false. I call the CancelAsync() however it doesn;t cancel anything
    with the backgroundworke r.

    What else can I do. I can;t set the .Isbusy because its read-only



    Public Sub StopAllDataBack groundThreads()
    Me.tmrBackgroun dWorker.Enabled = False
    Me.tmrBackgroun dWorker2.Enable d = False

    If Not IsNothing(Me.da taBackgroundWor ker) Then
    If dataBackgroundW orker.IsBusy Then
    Me.dataBackgrou ndWorker.Cancel Async()
    While dataBackgroundW orker.IsBusy
    Application.DoE vents()
    Me.dataBackgrou ndWorker.Cancel Async()
    End While
    Me.dataBackgrou ndWorker.Dispos e()
    End If
    End If

    If Not IsNothing(Me.da taBackgroundWor ker2) Then
    If dataBackgroundW orker2.IsBusy Then
    Me.dataBackgrou ndWorker2.Cance lAsync()
    While dataBackgroundW orker2.IsBusy
    Application.DoE vents()
    Me.dataBackgrou ndWorker2.Cance lAsync()
    End While
    Me.dataBackgrou ndWorker2.Dispo se()
    End If
    End If
    End Sub
  • Armin Zingler

    #2
    Re: Multithreading Race Conditions

    "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
    Im having an issue setting back my databackgroundw orker.is busy to
    false. I call the CancelAsync() however it doesn;t cancel anything
    with the backgroundworke r.
    >
    What else can I do. I can;t set the .Isbusy because its read-only
    >
    >
    >
    Public Sub StopAllDataBack groundThreads()
    Me.tmrBackgroun dWorker.Enabled = False
    Me.tmrBackgroun dWorker2.Enable d = False
    >
    If Not IsNothing(Me.da taBackgroundWor ker) Then
    If dataBackgroundW orker.IsBusy Then
    Me.dataBackgrou ndWorker.Cancel Async()
    While dataBackgroundW orker.IsBusy
    Application.DoE vents()
    Me.dataBackgrou ndWorker.Cancel Async()
    End While

    Read the first two paragraphs in the Remarks section:
    http://msdn.microsoft.com/en-us/libr...ncelasync.aspx


    Does your backgroundworke r check the CancellationPen ding property? If not,
    you have to add it and exit the method(s) if it is True.


    Armin

    Comment

    • cmdolcet69

      #3
      Re: Multithreading Race Conditions

      On Oct 31, 3:21 pm, "Armin Zingler" <az.nos...@free net.dewrote:
      "cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
      >
      >
      >
      Im having an issue setting back my databackgroundw orker.is busy to
      false. I call the CancelAsync() however it doesn;t cancel anything
      with the backgroundworke r.
      >
      What else can I do. I can;t set the .Isbusy because its read-only
      >
      Public Sub StopAllDataBack groundThreads()
             Me.tmrBackgroun dWorker.Enabled = False
             Me.tmrBackgroun dWorker2.Enable d = False
      >
             If Not IsNothing(Me.da taBackgroundWor ker) Then
                 If dataBackgroundW orker.IsBusy Then
                     Me.dataBackgrou ndWorker.Cancel Async()
                     While dataBackgroundW orker.IsBusy
                         Application.DoE vents()
                         Me.dataBackgrou ndWorker.Cancel Async()
         End While
      >
      Read the first two paragraphs in the Remarks section:http://msdn.microsoft.com/en-us/libr...tmodel.backgro...
      >
      Does your backgroundworke r check the CancellationPen ding property? If not,
      you have to add it and exit the method(s) if it is True.
      >
      Armin
      Check what you had asked I see that the .IsBusy property never gets
      set back to false its always true. Therefore I dont think
      the .CancelAsync() is working? THe collection pending is False

      Comment

      • Mark S. Milley

        #4
        Re: Multithreading Race Conditions

        There is a property setting that you need to turn on.

        I believe it's "SupportsCancel lation", but I'm not sure.

        Regards,

        -M

        Comment

        • Armin Zingler

          #5
          Re: Multithreading Race Conditions

          "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
          Read the first two paragraphs in the Remarks
          section:http://msdn.microsoft.com/en-us/libr...tmodel.backgro...
          >
          Does your backgroundworke r check the CancellationPen ding property? If not,
          you have to add it and exit the method(s) if it is True.
          >
          Armin
          Check what you had asked I see that the .IsBusy property never gets
          set back to false its always true. Therefore I dont think
          the .CancelAsync() is working? THe collection pending is False


          ==========

          I forgot to say that the WorkerSupportsC ancellation property must
          also be True. But it seems it is, otherwise you'd get an exception.


          Try this in a new project. Works for me:

          Imports System.Componen tModel

          Public Class Form1

          Private Sub Form1_Load( _
          ByVal sender As System.Object, ByVal e As System.EventArg s) _
          Handles MyBase.Load

          Dim bgw As New BackgroundWorke r

          bgw.WorkerSuppo rtsCancellation = True
          AddHandler bgw.DoWork, AddressOf OnDoWork
          AddHandler bgw.RunWorkerCo mpleted, AddressOf OnWorkerComplet ed

          Debug.Print("St arting worker")
          bgw.RunWorkerAs ync()

          Debug.Print("Wa iting 5 seconds...")
          Threading.Threa d.Sleep(5000)
          Debug.Print("Ca ncelling worker")
          bgw.CancelAsync ()

          End Sub

          Private Sub OnDoWork( _
          ByVal sender As Object, _
          ByVal e As System.Componen tModel.DoWorkEv entArgs)

          Debug.Print("St art OnDoWork")
          Do
          Threading.Threa d.Sleep(250)
          Loop Until DirectCast(send er, BackgroundWorke r).Cancellation Pending
          Debug.Print("En d OnDoWork")

          End Sub

          Private Sub OnWorkerComplet ed( _
          ByVal sender As Object, _
          ByVal e As System.Componen tModel.RunWorke rCompletedEvent Args)

          Debug.Print("Wo rker completed")

          End Sub


          End Class



          Armin

          Comment

          Working...