Chris Sansone
Chris Sansone

Reputation: 231

Get information about internal state of Windows Service

I have a Windows Service that I am writing in .NET C#. The service is going to act as a file processor. It just watches a directory for File Created events and adds these files to a queue for processing. A separate thread pulls files from the queue and processes them.

My question is whether there is a way to query the windows service to get its "state". I would like to be able to query the service some way and see a list of the files currently waiting in the queue etc.

I know this can be done in Linux via the /proc file system and I was wondering if there is anything similar for Windows. Any tips/pointers would be greatly appreciated.

Upvotes: 6

Views: 1639

Answers (3)

sgmoore
sgmoore

Reputation: 16077

If you are looking for a non-UI method (eg to write the names to a file or to standard output), it is possible to use the ExecuteCommand Method on the service.

  ServiceController sc = new ServiceController("ServiceName");
  sc.ExecuteCommand(255);

This simply passes this command to your your service and your service will handle it via the OnCustomCommand

protected override void OnCustomCommand(int command)
{
  base.OnCustomCommand(command);
  if (command == 255
  {
      ... // Your code here
  }
}

You may need to store your queue/service status in a static variable so you can access it from the OnCustomCommand routine.

Upvotes: 3

paramosh
paramosh

Reputation: 2258

WCF would be good to do that, especially it can be hosted inside of Windows Service. Might be in your case it makes sense to use XML-RPC with WCF

Upvotes: 0

Erix
Erix

Reputation: 7105

You could create a hosted WCF service inside of the windows service with whatever methods you need to access the state.

http://msdn.microsoft.com/en-us/library/ms733069.aspx

Upvotes: 2

Related Questions