How to Cancel a WMI Query

Discussion in 'Microsoft VB .NET' started by bandwood1@gmail.com, Apr 20, 2012.

  1. Guest

    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.
     
    , Apr 20, 2012
    #1
    1. Advertisements

  2. GS Guest

    After serious thinking 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
     
    GS, Apr 20, 2012
    #2
    1. Advertisements

  3. Guest

    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 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
     
    , Apr 26, 2012
    #3
  4. GS Guest

    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


    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 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
     
    GS, Apr 26, 2012
    #4
  5. Guest

    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
    >
    >
    > 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 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.
     
    , Apr 27, 2012
    #5
  6. GS Guest

    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
     
    GS, Apr 27, 2012
    #6
  7. GS Guest

    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, Apr 27, 2012
    #7
  8. Tom Shelton Guest

    GS submitted this idea :
    > After serious thinking 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
     
    Tom Shelton, May 8, 2012
    #8
  9. Tom Shelton Guest

    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
     
    Tom Shelton, May 8, 2012
    #9
  10. GS Guest

    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
     
    GS, May 9, 2012
    #10
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thunder$truck

    Process Thread Count Query in WMI

    Thunder$truck, Jan 6, 2004, in forum: Microsoft VB .NET
    Replies:
    0
    Views:
    752
    Thunder$truck
    Jan 6, 2004
  2. HankC

    WMI query for events

    HankC, Sep 26, 2005, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    383
    HankC
    Sep 28, 2005
  3. Spam Catcher

    .NET & WMI & SNMP -> Writing Values to WMI

    Spam Catcher, Dec 17, 2006, in forum: Microsoft VB .NET
    Replies:
    0
    Views:
    279
    Spam Catcher
    Dec 17, 2006
  4. NetworkElf

    WMI query

    NetworkElf, May 8, 2007, in forum: Microsoft VB .NET
    Replies:
    8
    Views:
    602
    NetworkElf
    May 10, 2007
  5. kimiraikkonen

    "Not Supported" error on WMI Query in VB.NET

    kimiraikkonen, Apr 24, 2008, in forum: Microsoft VB .NET
    Replies:
    0
    Views:
    1,220
    kimiraikkonen
    Apr 24, 2008
Loading...

Share This Page