SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - WMI Programming with Visual Basic.NET: Tra...
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: Trapping System Events
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2005-07-25

    Table of Contents:
  • WMI Programming with Visual Basic.NET: Trapping System Events
  • Creating a simple application
  • What is “WqlEventQuery”?
  • Watching the event

  • 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    WMI Programming with Visual Basic.NET: Trapping System Events - Creating a simple application


    (Page 2 of 4 )

    Instead of talking too much, let us do it practically. I would like to develop a simple Windows application, which starts to listen for only a single event. Once it receives the notification of a specified event instance, it just displays the status (or information) of that event and stops listening for that event any more. We start with this simple concept, and later develop complicated applications.

    So, let us start by opening the Visual Studio.NET 2003 Enterprise Architect. Create a Windows application with only a single button 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):

    Copy the following code into the button click event:

    Me.Button1.Enabled =False

    DimqueryAsNewWqlEventQuery( _

                     "__InstanceModificationEvent", _

                    NewTimeSpan(0, 0, 1), _

                     "TargetInstance isa ""Win32_Service""")

           DimwatcherAsNewManagementEventWatcher(query)

           DimevAsManagementBaseObject = watcher.WaitForNextEvent()

     

    Me.TextBox1.Text &= ControlChars.NewLine & _

                    "Service’" &CType(ev("TargetInstance"), ManagementBaseObject)("DisplayName") & "’has changed, State is’" &CType(ev("TargetInstance"), ManagementBaseObject)("State") & "’"

     

            watcher.Stop()

    Don’t try to understand the above code yet. Just execute it first.  Press the button after you have executed your application. It starts listening for the event. To notify the listener, open “services.msc” from Start->Run and change any status of any process (choose a generic process rather than harmful system process like RPC). I generally use “Indexing Service” for demonstration purposes, and I suggest you do the same (unless it is not installed on your computer). When you change the status, you should be able to see the textbox updated accordingly as shown in the following figure (fig 2).

    The listener stops listening once it receives the notification of event.

    You can obtain the above code from the  “form1.vb” file in the zip included for download. The coming sections give you a complete, detailed explanation of the above program.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


     

    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 5 hosted by Hostway