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?
Next: Is there any other good example to work with? >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee