Solved

VB.Net Backgroundworker not cancelling

Posted on 2016-09-04
4
39 Views
Last Modified: 2016-09-04
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

0
Comment
Question by:PNRT
  • 2
  • 2
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41783742
The test for cancelation should be in the For Next loop. Where you have it, it will be tested once at the end of the DoWork method at which point all work has been done. Also if the Search calls take a while the cancelation will not happen until it is completed.
0
 
LVL 2

Author Comment

by:PNRT
ID: 41783744
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?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41783746
Yes that is correct.
0
 
LVL 2

Author Comment

by:PNRT
ID: 41783747
Many Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question