Avatar of PNRT
PNRT
 asked on

VB.Net Backgroundworker not cancelling

Hi Experts can someone help with this please?

I'm running this code on a backgroundworker with a button for  BackgroundWorker1.CancelAsync() and BackgroundWorker1.WorkerSupportsCancellation is set to True.   I also have two labels showing on a timer -  BackgroundWorker1.CancellationPending True/False  and  BackgroundWorker1.IsBusy True/False

When I cancel the backgroundworker, the CancellationPending shows true after about 2 seconds but the process still continues and theBackgroundWorker1.IsBusy label confirms it.    It eventually cancels itself after about two minutes which is the same time that it takes to finish normally

The delay never gets to the For/Next Loop but is in the  two lines
updateSearcher.Search("IsInstalled=0 and Type='Software'")
searchResult = updateSearcher.Search("IsInstalled=0")

I've tried     While BackgroundWorker1.CancellationPending = False  but no change

I'm assuming that while these two lines are processing, it cannot check for the CancellationPending but don't know how to force this.  Anyone any ideas please?



Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

            Dim updateSearcher As New WUApiLib.UpdateSearcher
            Dim updateSession As New WUApiLib.UpdateSessionClass

            updateSearcher.Search("IsInstalled=0 and Type='Software'")
            Dim searchResult As WUApiLib.ISearchResult
            Dim Update As WUApiLib.IUpdate

            searchResult = updateSearcher.Search("IsInstalled=0")

            Dim i As Integer = 0
            For i = 0 To searchResult.Updates.Count - 1
          ''Updates stuff     
            Next
     
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True
        End If
    End Sub

Open in new window

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
PNRT

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PNRT

ASKER
Thank you for the reply

As I mentioned, the delay is in the search not the for/next loop.    So I understand that there is no way to stop it before then - is that correct?
Fernando Soto

Yes that is correct.
PNRT

ASKER
Many Thanks
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck