SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - 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 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    WMI Programming with Visual Basic.NET: More About Trapping System Events - Creating a better application


    (Page 2 of 4 )

    I would like to develop a simple Windows application, which starts to listen (watch for events) forever (permanent subscriber). Once it receives a notification of a specified event instance, it just displays the status (or information) of that event and continues listening (watching) for the next event. This is what we call a permanent subscriber.

    So, let us start by opening the Visual Studio.NET 2003 Enterprise Architect. Create a Windows application with two buttons and a
    multi-lined textbox. Don’t forget to add the reference to “System.Management” (as explained in part one) and import it into your application. Your design should look something like the following figure (Fig 1):

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

    DimwatcherAsManagementEventWatcher

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

    Dim query As WqlEventQuery = New WqlEventQuery( _
      
    "__InstanceModificationEvent", _
       
    NewTimeSpan(0, 0, 1), _
        "TargetInstance isa ""Win32_Service""")
    watcher =New ManagementEventWatcher(query)
    AddHandler watcher.EventArrived, AddressOf HandleEvent

    ' Start listening
    watcher.Start()

    Add the following event handler:

    Public Sub HandleEvent(ByVal sender As Object, _
        ByVal e As EventArrivedEventArgs)
     
    Dim ev As ManagementBaseObject = e.NewEvent
      TextBox1.Text &= ControlChars.NewLine & _
        "Service '" &CType(ev("TargetInstance"),
    ManagementBaseObject)("DisplayName") & "' has changed,
    State is '" &CType(ev("TargetInstance"),
    ManagementBaseObject)("State") & "'"

    EndSub

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

    watcher.Stop()

    That’s it. We are finished. Try executing it. Press the start button after you execute your application. It starts listening to events. To notify the listener, open “services.msc” from Start->Run and change the status of any process (choose a generic process rather than harmful system process like RPC).  I generally use “Indexing Service” for my demonstration purpose and I suggest the same (unless installed on your computer). When you change the status, you should be able to see the textbox updated accordingly. Observe that it can listen to any number of events. Finally stop the subscription of events by clicking on button “stop”.

    There exists nothing new in this application apart from handling the event through a dedicated handler (which is the concept of “adding event handlers” in OOP using .NET). Earlier we used to work with the method “WaitForNextEvent” method, which works only for a single (temporary) event and hangs our application till it receives the event.  Now we removed that concept totally. We are not “waiting” anymore.  We are handling if and only if it occurs. We don’t hang our application (like my earlier version) for the events to come in. Don’t you think this is better?

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


     

    VISUAL BASIC.NET ARTICLES

    - 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...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic





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