Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net Backgroundworker not cancelling

Posted on 2016-09-04
4
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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