有一个.NET程序集,它需要同时运行3个实例,我该如何监控它。
我相当肯定这可以通过监控系统进程来完成?
发布于 2010-03-27 07:08:32
类似这样的:
using System.Diagnostics;
// ...
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc);
if (processes.Length != 3)
{
// ...
}发布于 2010-03-27 07:15:24
步骤1:启动初始进程,并让它知道它是主"ID1"
要启动初始进程,请使用命令行参数。MyProcess.exe -ID1流程的这个实例知道它是“主”或ID1 vs. ID2,ID3
步骤2:当应用程序的ProcessID为#1时,定期运行此代码
注意:您可以使用计时器并激发事件,并使用以下代码处理它:
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备注:
https://stackoverflow.com/questions/2527338
复制相似问题