Solved

VB.Net Backgroundworker not cancelling

Posted on 2016-09-04
4
42 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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