There's a .NET assembly and it needs to have 3 instances running at the same time all the time how can I monitor this.
I am fairly positive 开发者_运维百科it can be done by monitoring the system processes?
Something like this:
using System.Diagnostics;
// ...
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length != 3)
{
// ...
}
Step 1: Start the initial process and let it know that it's the master "ID1"
To start the initial process, use a command line argument. MyProcess.exe -ID1 This instance of your process knows that it is the "master" or ID1 vs. ID2, ID3
Step 2: Run this code periodically when the application's ProcessID is #1
Note: You can use a timer and fire an event and handle it with the code below:
Imports System.Diagnostics
'Check to see if we need to spawn one or more processes
Dim ProcessCounter as integer = 0
For Each p as Process In Process.GetProcesses
if p.NameProperty??.ToString() = "MyProcessName" then ProcessCounter += 1
Next
'Use this code to spawn new instances of the process, and assign process ID's accordingly
while processcounter < 3
Use Process.Start() and run a new instance of your process, but pass it a command line argument -ID# where # is the # of that process (also = to ProcessCounter)
ProcessCounter += 1
end while
Notes:
- You may want to run a "watcher" process that periodically runs the above code... you can have a windows task that runs a simple exe every minute or so that contains only the above code. You must use a "watcher" process if the master process (or the one with ID = 1 above) may terminate
- If a process has an internal ID variable that is not 1 (indicating that it is the master process that is in charge of monitoring for the existing of 3 instances), then the above code will not be run. Only one of the processes needs to do this monitoring
精彩评论