简体   繁体   中英

Designing a Process Monitor Windows Service

I'm designing a Windows Service that maintains a list of processes. It can run tasks on them. For example - stop a specific process (within its list), restart a specific process, run a new process and add it to the list, etc.

This Windows Service is controlled by a web app, through a WCF service - the Windows Service runs a WCF service, and the web app connects to that WCF service. The WCF service exports methods such as Restart(int id) and Stop(int id) .

I have several questions:

  • When a process unexpectedly closes (for example, because of an internal error), the service should update a value in some database. Right now, to do this, I'm just having a thread that every 30 seconds it checks if the process is running or not.

    Is this the right to do this? Should I use the Exit event?

  • When the service closes, for any reason, all of the processes that it maintains shall close too. What is the right way to do this?

    I was thinking about just loop through the list and kill all processes when the service's OnClose() is called. The problem with this is that the OnClose() event is not always being called. For example, what if the service unexpectedly closes? It won't call OnClose() .

  • How should I represent each task (for example - restart a process or stop a process)?

    Right now I'm using System.Threading.Tasks . Is it the right way?

This service should be highly scalable, with minimum mistakes.

Thanks.

For closing the processes when the service closes, i think following steps can do the job for you.

  1. Write a small program which can close the processes you want to close.
  2. Go to Service Control Manager and right click on your service.
  3. Go to Recovery tab and specify your program for execution under First Failure, Second Failure and Subsequest Failure listboxs.

Hope this would help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM