Solved

VB.Net Backgroundworker not cancelling

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

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 1

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 62

Expert Comment

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

Author Comment

by:PNRT
ID: 41783747
Many Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now