Multithreading Race Conditions

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

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

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



Public Sub StopAllDataBackgroundThreads()
Me.tmrBackgroundWorker.Enabled = False
Me.tmrBackgroundWorker2.Enabled = False

If Not IsNothing(Me.dataBackgroundWorker) Then
If dataBackgroundWorker.IsBusy Then
Me.dataBackgroundWorker.CancelAsync()
While dataBackgroundWorker.IsBusy
Application.DoEvents()
Me.dataBackgroundWorker.CancelAsync()
End While
Me.dataBackgroundWorker.Dispose()
End If
End If

If Not IsNothing(Me.dataBackgroundWorker2) Then
If dataBackgroundWorker2.IsBusy Then
Me.dataBackgroundWorker2.CancelAsync()
While dataBackgroundWorker2.IsBusy
Application.DoEvents()
Me.dataBackgroundWorker2.CancelAsync()
End While
Me.dataBackgroundWorker2.Dispose()
End If
End If
End Sub
 
cmdolcet69 said:
Im having an issue setting back my databackgroundworker.is busy to
false. I call the CancelAsync() however it doesn;t cancel anything
with the backgroundworker.

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



Public Sub StopAllDataBackgroundThreads()
Me.tmrBackgroundWorker.Enabled = False
Me.tmrBackgroundWorker2.Enabled = False

If Not IsNothing(Me.dataBackgroundWorker) Then
If dataBackgroundWorker.IsBusy Then
Me.dataBackgroundWorker.CancelAsync()
While dataBackgroundWorker.IsBusy
Application.DoEvents()
Me.dataBackgroundWorker.CancelAsync()
End While


Read the first two paragraphs in the Remarks section:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx


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


Armin
 
Read the first two paragraphs in the Remarks section:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgro...

Does your backgroundworker check the CancellationPending 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
 
There is a property setting that you need to turn on.

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

Regards,

-M
 
cmdolcet69 said:
Read the first two paragraphs in the Remarks
section:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgro...

Does your backgroundworker check the CancellationPending 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 WorkerSupportsCancellation 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.ComponentModel

Public Class Form1

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

Dim bgw As New BackgroundWorker

bgw.WorkerSupportsCancellation = True
AddHandler bgw.DoWork, AddressOf OnDoWork
AddHandler bgw.RunWorkerCompleted, AddressOf OnWorkerCompleted

Debug.Print("Starting worker")
bgw.RunWorkerAsync()

Debug.Print("Waiting 5 seconds...")
Threading.Thread.Sleep(5000)
Debug.Print("Cancelling worker")
bgw.CancelAsync()

End Sub

Private Sub OnDoWork( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs)

Debug.Print("Start OnDoWork")
Do
Threading.Thread.Sleep(250)
Loop Until DirectCast(sender, BackgroundWorker).CancellationPending
Debug.Print("End OnDoWork")

End Sub

Private Sub OnWorkerCompleted( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)

Debug.Print("Worker completed")

End Sub


End Class



Armin
 
Back
Top