WMI Programming with Visual Basic.NET: More About Trapping System Events - Is there any other good example to work with?
(Page 3 of 4 )
Why not? We can develop several other examples with the same concept. Until now, I have always been working with the “Win32_Service” class only. Not let us start with another class, “Win32_Process”. Part three and part five of this series have already discussed this class (but not to the events level). Now, I would like to modify the above created application, so that it subscribes to the class “Win32_Process”.
You need to modify the WQL query as follows:
Dim query As WqlEventQuery = New WqlEventQuery( _
"__InstanceCreationEvent", _
New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")
And the event handler as following:
Public Sub HandleEvent(ByVal sender As Object, ByVal _
e As EventArrivedEventArgs)
Dim ev AsManagementBaseObject = e.NewEvent
TextBox1.Text &= ControlChars.NewLine & _
"Process '" &CType(ev("TargetInstance"), ManagementBaseObject)("Name") & "' started"
TextBox1.Refresh()
EndSub
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 any application such as “notepad”, “calculator”, “PaintBrush” etc. When you open any application, the WMI treats it as a new process and notifies our application of the event. You should be able to observe that the textbox gets updated accordingly (with “process started” messages). Finally stop the subscription of events by clicking on button “stop”. Is it not interesting?
Did you observe the above code carefully? I replaced “__InstanceModificationEvent” with “__InstanceCreationEvent”. This is very important as we are “creating” (opening) a new process, but not modifying an existing process.
Next: What about __InstanceDeletionEvent? >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee