Start Background Worker

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

How do i once i have cancelled a background worker run it again?

Thanks.

Also:

How would i get this to work at 70% of the cpu instead of 100%.

Thanks.
 
Last edited:
Just start RunWorkerAsync method again.
You can't control the ThreadPriority with BackgroundWorker, to do that you must set up regular multithreading. The exact CPU utilization can't be set, only choosing from the enumerated priority modes listed.
 
You can check IsBusy. Example:
VB.NET:
bw.CancelAsync()
Do While bw.IsBusy
  Application.DoEvents()
Loop
bw.RunWorkerAsync()
 
Yeah thats what i tried first. Doesnt work, seems to do an infinite loop on the do just going around and around.
 
Does work here, but do you check the BW.CancellationPending property in DoWork event handler method? The work doesn't stop by itself, you have to check "periodically" this property and stop the work if the caller request it cancelled.
 
Does work here, but do you check the BW.CancellationPending property in DoWork event handler method? The work doesn't stop by itself, you have to check "periodically" this property and stop the work if the caller request it cancelled.

Yeah, I have a latch for the the process' in Background Worker, it checks if the cancel is pending first and then does its shizzle and if the cancellationpending is true then it cancels the background worker.
 
Thought it might help if i posted my code. This is basically just a test application to find prime numbers:

VB.NET:
    Delegate Sub AddtoListCallback(ByVal [text] As String)
    Dim lastNumber As Double

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        If btnFind.Text = "Continue..." Then

            btnStop.Enabled = True
            btnReset.Enabled = False

            btnFind.Enabled = False
            btnFind.Text = "Finding..."

            bgPrimeFinder.RunWorkerAsync()

        Else

            primeList.Items.Clear()
            primeList.Items.Add("2")
            primeList.Items.Add("3")
            primeList.Items.Add("5")
            primeList.Items.Add("7")
            primeList.Items.Add("11")

            btnStop.Enabled = True

            btnFind.Enabled = False
            btnFind.Text = "Finding..."

            lastNumber = 13

            bgPrimeFinder.RunWorkerAsync()

        End If

    End Sub

    Private Sub bgPrimeFinder_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgPrimeFinder.DoWork

        For testNumber As Double = lastNumber To Double.PositiveInfinity

            If bgPrimeFinder.CancellationPending = False Then

                If testNumber.ToString.EndsWith("0") Or testNumber.ToString.EndsWith("2") Or testNumber.ToString.EndsWith("4") Or testNumber.ToString.EndsWith("5") Or testNumber.ToString.EndsWith("6") Or testNumber.ToString.EndsWith("8") Then

                    'Do Nothing

                Else

                    Dim primeNumberFlag As Boolean

                    For Each currentPrime As Double In primeList.Items

                        If currentPrime > (testNumber / 2) Then

                            Exit For

                        Else

                            If (testNumber / currentPrime).ToString.Contains(".") Then

                                primeNumberFlag = True

                            Else

                                primeNumberFlag = False
                                Exit For

                            End If

                        End If

                    Next

                    If primeNumberFlag = True Then

                        AddtoList(testNumber.ToString)

                    End If

                End If

            Else

                lastNumber = testNumber
                e.Cancel = True

            End If

        Next

    End Sub

    Private Sub AddtoList(ByVal [text] As String)

        If primeList.InvokeRequired Then

            Dim d As New AddtoListCallback(AddressOf AddtoList)
            Invoke(d, New Object() {[text]})

        Else

            primeList.Items.Add([text])
            primeList.SelectedItem = [text]

        End If

    End Sub
 
Almost, but you don't break the loop, after setting e.Cancel (which is a notification message for the RunWorkerCompleted event) you stop the loop with 'Exit For' statement.
 
I could of sworn i put that, but maybe that was just the earlier bit :(

Anyways thanks for your help.

I now have another issue, because the background worker is using all the cpu the ui is really slow and crashes, is there anyway to make this not so?

Thanks.
 
What you can do is slowing the worker by making each iteration sleep a short while, for example 1 milliseconds: Threading.Thread.Sleep(1)

Your current worker thread does not slow the UI any more than it slows the whole computer, because worker thread only touches main thread when AddtoList is invoked, which in your case never happens.
 
Back
Top