Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 673
Chris Sansone Avatar asked May 21 '12 17:05

Chris Sansone


People also ask

How can I tell if a Windows service is running command line?

Top Windows command-line commands Anytime you want to know what services are installed on a computer and find out which ones are active, you can use sc query state= all to find a complete list. If the computer in question is remote, you should use sc \\computername query state= all.

How do I find Windows services?

Press the Win + R keys on your keyboard, to open the Run window. Then, type "services. msc" and hit Enter or press OK. The Services app window is now open.

How do I debug a Windows local service?

In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button. The Processes dialog box appears. Select the Show processes from all users check box. In the Available Processes section, choose the process for your service, and then choose Attach.

How do I debug a Windows service without installing?

The original generated Main() is like the one below. To debug without installing, add the code as follow, mainly #if (! DEBUG). You will be able to step through the code when you run the Windows Service without installing.


2 Answers

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.

like image 69
sgmoore Avatar answered Oct 12 '22 10:10

sgmoore


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

like image 29
Erix Avatar answered Oct 12 '22 09:10

Erix