(E-Mail Removed) explained on 4/19/2012 :
> Hi,
>
> I have initiated a WMI query, see below;
>
> Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT *
> FROM Win32_Product") For Each queryObj As ManagementObject In
> searcher.Get() ..... DO WORK HERE Next
>
> How to I abort this Get call because it takes approx 60 seconds on my
> comuter (I know the time will differ between computers) but I need to
> be able to cancel it.
>
> Note, putting a exit look does not help because it does not enter the
> loop until after the query has executed.
>
>
> Any ideas?
>
> Thanks,
> Dennis.
Dennis - perform the query async. Look at the Get overload that takes
ManagementOperationObserver. It provides a cancel method...
Here is a very simple example:
Option Explicit On
Option Strict On
Option Infer On
Imports System.Management
Module Module1
Private done As Boolean
Sub Main()
Dim a As New ManagementOperationObserver
AddHandler a.ObjectReady, AddressOf ObjectReady
AddHandler a.Completed, AddressOf Completed
Using server = New ManagementObjectSearcher("root\CIMV2",
"SELECT * FROM Win32_Product")
server.Get(a)
Console.ReadLine()
a.Cancel()
End Using
End Sub
Private Sub ObjectReady(ByVal sender As Object, _
ByVal e As ObjectReadyEventArgs)
Console.WriteLine("Name : {0}", e.NewObject("Name"))
End Sub
Private Sub Completed(ByVal sender As Object, ByVal e As
CompletedEventArgs)
done = True
End Sub
End Module
Set a breakpoint on the Console.ReadLine - your code will go right to
that. If you hit enter, and then continue to single step - you will
hit the a.cancel - and the completed event will fire immediately.
--
Tom Shelton