Monitoring the activity of a .net windows service

  • Thread starter Thread starter TG
  • Start date Start date
T

TG

I have created a .net windows service which polls a
directory for files every so often. I have created a
client service controller application which is used to
set configuration properties for the windows service. But
in this client application i also want to be able to
determine the status of the windows service. Not the
running, paused, or stopped status, but whether the
service is currently processing a file. SO what I ideally
want is for the service to fire and event which the
client can register to everytime the service processes a
file. Is this possible? ANy help on how this could be
acheived would be extremely grateful.

Thanks
T
 
I've done almost exactly the same thing (my client controller looks after a
number of services). I'm
using WMI to communicate.

The managed wmi classes in System.Management make it really easy to achieve.

--pat
 
Do you happen to know any good sources of information on
using the classes in System.Management. Or somewhere with
code examples. I looked at msdn but found it difficult as
I expect it assumes some prior knowledge on WMI.

Thanks
T
 
Hey Tony.

I found the same thing, but to be honest, the documentation is a little
light on as far
as complete working samples are concerned.


Very simply (what I did), you can publish WMI events (code in VB):

1) Indicate that the assembly has been WMI instrumented (in the
AssemblyInfo.vb or .cs)

' This Assembly has been WMI instrumented
<Assembly:
System.Management.Instrumentation.Instrumented("root\ORGANISATION\PRODUCT")>

2) Define an instrumented WMI event that inherits from
System.Management.Instrumentation.BaseEvent

<System.Management.Instrumentation.InstrumentationClass(Management.Instrumen
tation.InstrumentationType.Event)> _
Public Class MyEvent
Inherits Management.Instrumentation.BaseEvent

Public SomeArbitraryData As Integer

Public Sub New(ByVal valSomeArbitraryData As Integer)
Me.SomeArbitraryData = valSomeArbitraryData
End Sub
End Class

3) Then somewhere in your server, publish the event:

Dim status As MyEvent
status = New MyEvent(3)
status.Fire()

The WMI event is now available to any WMI client application that subscribes
to the
event you have defined.

4) In the client:
Dim theMyEventWatcher As Management.ManagementEventWatcher
Dim theQuery As New Management.EventQuery("SELECT * FROM MyEvent")
Dim theScope As New Management.ManagementScope("\\" & Me.myServerName &
"\root\ORGANISATION\PRODUCT")
Me.myStatusWatcher = New Management.ManagementEventWatcher(theScope,
theQuery)
Me.myStatusWatcher.Options.BlockSize = 1
AddHandler theStatusWatcher.EventArrived, AddressOf WMI_MyEvent

theStatusWatcher.Start()

Private Sub WMI_MyEvent(ByVal sender As Object, ByVal e As
Management.EventArrivedEventArgs)
Dim idx As Integer = e.NewEvent("SomeArbitraryData")

Select Case idx
Case 0
'

Case 1
'

Case Else
'
End Select
End Sub

There is a VS.Net add-in that I **highly** recommend you install, if you're
going to
be playing with WMI. Once you have defined and published your WMI events
and
data, you can use the add-in to query it - very useful.

That's a basic run-down anyway, and to be honest, I found out a lot of what
I did by
trial and error.

--pat
 
Thanks Pat, that was excellent help.

The problem i was having was with the select query, i
didnt understand how to query the event, but i do now.

Any idea how to remove these wmi classes as i was testing
and created stuff i would like to remove.

T
 
No problem. I had exactly the same trouble with the queries myself, not
having used WMI
before.

To remove old classes, use wbemtest.exe.

--pat
 
Back
Top