473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multithreading Race Conditions

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
Oct 31 '08 #1
4 2611
"cmdolcet69" <co************@hotmail.comschrieb
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/libr...ncelasync.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

Oct 31 '08 #2
On Oct 31, 3:21*pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
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/libr...tmodel.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
Oct 31 '08 #3
There is a property setting that you need to turn on.

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

Regards,

-M

Oct 31 '08 #4
"cmdolcet69" <co************@hotmail.comschrieb
Read the first two paragraphs in the Remarks
section:http://msdn.microsoft.com/en-us/libr...tmodel.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

Oct 31 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: GIMME | last post by:
One of my coworkers insists that one should never use static methods because race conditions exist. Thinking along the lines that all variable assignments are assignments to static variables. ...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
4
by: Boni | last post by:
Dear all, Is there some trick to put controls on a particular control but in other thread. Example: A control is a part of bigger application and application thread freezes somtimes. So a...
2
by: manuelg | last post by:
Here is a code fragment, where I am trying to copy a file, avoiding overwrites and race conditions. The filename gets a '02','03','04' etc appended to the end if a file with that name already...
5
by: mars | last post by:
I use TurboGears to do some web service. TurboGears use cherrypy. When web browser access this site, the cherrypy will call my python program. So my program looks like a lib. When web browser...
1
by: Larry Bates | last post by:
I have a need to implement a drop folder upload mechanism for secure uploading of files to a server. At first glance this appears that it would be an easy application to write. Then I begin to...
0
by: Lars Uffmann | last post by:
<- about to have a major crisis here. All I wanted is to create a little tool with a GUI. Now I see problems related to thread programming arising everywhere. If I use the GUI to set a flag as a...
0
by: Cilk | last post by:
We at Cilk Arts are soon (Jan '09) going to release Cilk++ 1.0, with the goal of delivering the easiest, quickest, and most reliable way to maximize application performance on multicore processors....
0
by: moltendorf | last post by:
I've been trying to find a suitable method for preventing race conditions in my own code. Currently I'm using a file and the flock function to prevent code in other threads from executing at the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.