PC Review


Reply
Thread Tools Rate Thread

How to Cancel a WMI Query

 
 
bandwood1@gmail.com
Guest
Posts: n/a
 
      20th Apr 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.
 
Reply With Quote
 
 
 
 
GS
Guest
Posts: n/a
 
      20th Apr 2012
After serious thinking (E-Mail 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


 
Reply With Quote
 
 
 
 
bandwood1@gmail.com
Guest
Posts: n/a
 
      26th Apr 2012
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.


On Saturday, April 21, 2012 6:32:55 AM UTC+10, GS wrote:
> After serious thinking (E-Mail 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


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      26th Apr 2012
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 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.
>
>
> On Saturday, April 21, 2012 6:32:55 AM UTC+10, GS wrote:
>> After serious thinking (E-Mail 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

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


 
Reply With Quote
 
bandwood1@gmail.com
Guest
Posts: n/a
 
      27th Apr 2012
Garry,

I

On Thursday, April 26, 2012 2:03:33 PM UTC+10, GS wrote:
> 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 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.
> >
> >
> > On Saturday, April 21, 2012 6:32:55 AM UTC+10, GS wrote:
> >> After serious thinking (E-Mail 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
>
> 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.
 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      27th Apr 2012
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


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      27th Apr 2012
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


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      8th May 2012
GS submitted this idea :
> After serious thinking (E-Mail 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.

>


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.

--
Tom Shelton


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      8th May 2012
(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


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      9th May 2012
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find a relation between WMI NetworkAdapter and WMI Modem Joe Windows XP WMI 0 18th May 2006 10:20 AM
WMI Error "Could not connect to WMI services =?Utf-8?B?QmlsbCBBcnRtYW4=?= Windows XP General 5 27th Jan 2006 01:47 AM
"Win32_Processor: WMI: Access denied" and "Win32_OperatingSystem: WMI: Access denied" Mark Jennings Windows XP WMI 3 29th Dec 2004 05:18 PM
Re: WMI Error : Unable to connect to wmi service "root\cimv2" wil_riker01 Windows XP Networking 0 11th Feb 2004 10:55 PM
C++ Wmi App to C# Wmi App - System.Mgmt Access Jerry Microsoft C# .NET 1 9th Jul 2003 09:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:55 PM.