SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - WMI Programming with Visual Basic.NET: Mor...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

WMI Programming with Visual Basic.NET: More About Trapping System Events
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2005-08-01

    Table of Contents:
  • WMI Programming with Visual Basic.NET: More About Trapping System Events
  • Creating a better application
  • Is there any other good example to work with?
  • What about __InstanceDeletionEvent?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    WMI Programming with Visual Basic.NET: More About Trapping System Events - What about __InstanceDeletionEvent?


    (Page 4 of 4 )

    I didn’t discuss this issue at all. I have discussed  “__InstanceModificationEvent” or “__InstanceCreationEvent”, but never touched “__InstanceDeletionEvent”.  Now let us modify the program discussed in the previous section in such a way that it handles “__InstanceDeletinEvent” related events.

    Modify your WQL query as follows:

    Dim query As WqlEventQuery =NewWqlEventQuery( _
      "__InstanceDeletionEvent", _
      New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")

    That’s it. I just replaced “__InstanceCreationEvent” with “__InstanceDeletionEvent”. You need not change anything else. Try executing it. Press the start button after you execute your application.  It starts listening to events. To notify the listener, open any application such as “notepad”, “calculator”, “PaintBrush” etc. When you close any of those applications, the WMI treats it as a process being deleted from memory and notifies our application of the event. You should be able to observe that the textbox gets updated accordingly (with “process stopped” messages). Finally stop the subscription of events by clicking on button “stop”.

    {mospagebreak title=Can we handle more than one type of event simultaneously?}

    Why not? You can handle as many types of events as possible. The sky is the limit. Now let us modify the previous application completely so that it listens to both “__InstanceCreationEvent” and “__InstanceDeletionEvent” together. Does that sound interesting?

    Before we begin, you need to remove the code (only what we added, excluding the IDE generated code) in our previous application. After removing the code, you can start as follows:

    Declare the following declaration at class level (above all methods or events but within the class definition):

      Dim watcherCreation As ManagementEventWatcher
    Dim watcherDeletion As ManagementEventWatcher

    Copy the following code into the button “start” click event:

    'Watcher for Creation
    Dim queryCreation As WqlEventQuery = _
       New WqlEventQuery( 
    "__InstanceCreationEvent", _
       NewTimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")
    watcherCreation = New _
       ManagementEventWatcher(queryCreation)

    AddHandler watcherCreation.EventArrived,
    AddressOf HandleCreationEvent

    'Watcher for Deletion
    Dim queryDeletion As WqlEventQuery = New _
       WqlEventQuery( 
    "__InstanceDeletionEvent", _
       NewTimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")
    watcherDeletion = New _
       ManagementEventWatcher(queryDeletion)
    AddHandler watcherDeletion.EventArrived,
    AddressOf HandleDeletionEvent

    ' Start listening
    watcherCreation.Start()
    watcherDeletion.Start()

    Add the following two event handlers:

    Public Sub HandleCreationEvent(ByVal sender As Object,
       ByVal e As EventArrivedEventArgs)
      Dim ev As ManagementBaseObject = e.NewEvent
      TextBox1.Text &= ControlChars.NewLine & _
       "Process '" &CType(ev("TargetInstance"), ManagementBaseObject)("Name") & "' started.."
      TextBox1.Refresh()
    EndSub

    Public Sub HandleDeletionEvent(ByVal sender As Object,
       ByVal e As EventArrivedEventArgs)
     
    Dim ev As ManagementBaseObject = e.NewEvent
      TextBox1.Text &= ControlChars.NewLine & _
       "Process '" &CType(ev("TargetInstance"), ManagementBaseObject)("Name") & "' stopped.."
      TextBox1.Refresh()

    EndSub

    Copy the following code into the button “stop” click event:

    watcherCreation.Stop()
    watcherDeletion.Stop()

    I don’t think I need to explain much about the above modifications. I just doubled each of the statements explained in the previous section to listen for two types of events.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway