How to Cancel a WMI Query

  • Thread starter Thread starter bandwood1
  • Start date Start date
B

bandwood1

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.
 
After serious thinking (e-mail address removed) wrote :
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.

Because you 'implicitly' created the object it will be destroyed when
it falls outside of the procedure you created it in. This will happen
when VB.Net gets around to it, which will require extra background
processing that causes your app to be less efficient than it could be
if you stop letting VB act implicitly.

I don't believe you can destroy it via the 'Set' statement and specify
'= Nothing' because you didn't implement good programming practice when
you created it.

Dim searcher As ManagementObjectSearcher, queryObj As Variant
Set searcher = New ManagementObjectSearcher(...)
For Each queryObj In searcher.Get()
'..do work here
Next 'queryObj
'Work's done so clean up...
Set searcher = Nothing 'destroy it and free its space in memory

However, that is certainly not how I code (or would code) for using
WMI, but that's just my preference...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Garry,

Thanks for your post and I understand what you are referring too. There is an actual Dispose method that I use, so

searcher.Dispose needs to be called before the set to nothing.
Set searcher = Nothing

This is not the issue but.

Using your code fragement and ammend as follows;

Set searcher = New ManagementObjectSearcher(...)
debug.print(Now)
For Each queryObj In searcher.Get() '#####
debug.print(Now)
'..do work here
Next 'queryObj

The time difference between the 2 debug.prints is the 30-90 seconds I was talking about.

I want to know how to abort the query while it is stuck on the command marked as ##### above.

Can you or anyone else help me here.

Thanks,
Dennis.
 
I don't understand the way you're using WMI. As I stated, I do it
different and there's no measureable wait time where you say it takes
your code 30-90 secs to run.

As for aborting the loop that's iterating the query, you need to
include a condition that you use to 'Exit For' out of the loop. This
might be when you find the queryObj that meets some criteria. For
example...

If queryObj = MyCriteria Then
'do work here
Exit For 'don't look at any more results
End If


(e-mail address removed) formulated the question :
Garry,

Thanks for your post and I understand what you are referring too. There is an
actual Dispose method that I use, so

searcher.Dispose needs to be called before the set to nothing.
Set searcher = Nothing

This is not the issue but.

Using your code fragement and ammend as follows;

Set searcher = New ManagementObjectSearcher(...)
debug.print(Now)
For Each queryObj In searcher.Get() '#####
debug.print(Now)
'..do work here
Next 'queryObj

The time difference between the 2 debug.prints is the 30-90 seconds I was
talking about.

I want to know how to abort the query while it is stuck on the command marked
as ##### above.

Can you or anyone else help me here.

Thanks,
Dennis.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Garry,

I

I don't understand the way you're using WMI. As I stated, I do it
different and there's no measureable wait time where you say it takes
your code 30-90 secs to run.

As for aborting the loop that's iterating the query, you need to
include a condition that you use to 'Exit For' out of the loop. This
might be when you find the queryObj that meets some criteria. For
example...

If queryObj = MyCriteria Then
'do work here
Exit For 'don't look at any more results
End If


(e-mail address removed) formulated the question :

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

have updated the code to reflect the way you recommend, such as

Dim Searcher As ManagementObjectSearcher, queryObj As ManagementObject
Searcher = New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Product")
Debug.Print ("1 -> " & Now)
For Each queryObj As ManagementObject In Searcher.Get()
Debug.Print (("2 -> " & Now)
..... DO WORK HERE
..... conditional Exit here.
Next
'Work's done so clean up...
searcher.Dispose()
Searcher = Nothing


So the Debug output is something like
1 -> 26/04/2012 10:43:08 AM
2 -> 26/04/2012 10:43:56 AM


I have tried this on 4 different Windows 7 macgines all with similar results.

When you say it runs without delay, have you tried the same query?

Thanks,
DEnnis.
 
Dennis,
I have an entirely different approach; ..I use standard VB query that
works in VB/VBA/VB.net. I'll run it for your Win32_Product class and
post my code. This class has 12 properties and so my code will simply
do a Debug.Print for each property and its value. You should be able to
adapt it to your needs from there...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Try...

Dim oWMI, vData, v
Set oWMI = GetObject("WinMgmts:")
Set vData = oWMI.ExecQuery("Select * from Win32_Product")
On Error Resume Next
For Each v In vData
Debug.Print "Caption: " & v.Caption
Debug.Print "Description: " & v.Description
Debug.Print "InstallDate: " & v.InstallDate
Debug.Print "InstallDate2: " & v.InstallDate2
Debug.Print "InstallLocation: " & v.InstallLocation
Debug.Print "InstallState: " & v.InstallState
Debug.Print "Name: " & v.Name
Debug.Print "PackageCache: " & v.PackageCache
Debug.Print "SKUNumber: " & v.SKUNumber
Debug.Print "Vendor: " & v.Vendor
Debug.Print "Version: " & v.Version
Next 'v
Set oWMI = Nothing: Set vData = Nothing

Note this code implements early binding and so requires a ref set to
'Windows WMI Scripting V1.2 Library'. I find this works better as using
late binding causes code to 'hang' for some reason.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
GS submitted this idea :
After serious thinking (e-mail address removed) wrote :

GS - you don't know anything about .NET obviously, so I'm not sure why
your are even attempting to answer this question.
Because you 'implicitly' created the object

No, not true. VB.NET does not work like that antiquated POS VB6. The
line:

Dim server As New XXXXX

is exactly equivalient to:
Dim server As X
Set server = New X

In VB.NET - the object is explicitly created and initialized
immediately.
it will be destroyed when
it falls outside of the procedure you created it in

No it won't.
. This will happen
when VB.Net gets around to it, which will require extra background
processing that causes your app to be less efficient than it could be
if you stop letting VB act implicitly.

LOL.. No.
I don't believe you can destroy it via the 'Set' statement and
specify '= Nothing' because you didn't implement good programming
practice when you created it.

Yes he did. And there is no Set statement in VB.NET. And, by the way,
setting a local object to nothing = almost always pointless. And that
goes for VB6 as well.
Dim searcher As ManagementObjectSearcher, queryObj As Variant
Set searcher = New ManagementObjectSearcher(...)
For Each queryObj In searcher.Get()
'..do work here
Next 'queryObj
'Work's done so clean up...
Set searcher = Nothing 'destroy it and free its space in memory

However, that is certainly not how I code (or would code) for using
WMI, but that's just my preference...

I would suggest the OP ignore you.
 
(e-mail address 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,
You are correct about my knowledge of .Net! That said, I respectfully
decline any further replies during my learning process...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Back
Top