运行过程时更改过程ID
我在C#中使用System.diagnostics
。我的问题是,我正在使用代码开始和杀死C#中的过程,并且运行良好。但是我的问题是我的过程ID在运行该过程时在之间发生了变化。是可能的。如果是,那么如何获得要杀死的实际过程ID。我不能按名称执行此操作,因为我有一个多个实例在一次不同的情况下运行,我只想在启动端口上杀死一个实例。
我的代码是:
Process p2 = new Process();
ProcessStartInfo processStartInfo2 =
new ProcessStartInfo(
unitoolLauncherExePath,
"options --port " + port);
p2.StartInfo = processStartInfo2;
p2.Start();
System.Threading.Thread.Sleep(1000);
int processId = p2.Id;
现在它将返回14823之类的东西,当我试图杀死它时,它已更改。
Process[] _proceses = null;
_proceses = Process.GetProcessesByName("UNIToolAPIServer");
foreach (Process proces in _proceses)
{
if (proces.Id == processId)
{
proces.Kill();
}
}
但是这里没有任何东西被杀死,因为没有上述ID的过程被获取。
I am using System.Diagnostics
in c#. My problem is I am using code for starting and killing the process in c# and it's working fine. But my question is my process id is changing in-between while running the process. Is that possible. If yes then how can I get the actual process id which I want to kill. I can't do it by name because I have multiple instance are running on different at a time and I want to kill only single instance on started port.
My code is :
Process p2 = new Process();
ProcessStartInfo processStartInfo2 =
new ProcessStartInfo(
unitoolLauncherExePath,
"options --port " + port);
p2.StartInfo = processStartInfo2;
p2.Start();
System.Threading.Thread.Sleep(1000);
int processId = p2.Id;
Now it will return something like 14823 and when I am trying to kill it it's changed.
Process[] _proceses = null;
_proceses = Process.GetProcessesByName("UNIToolAPIServer");
foreach (Process proces in _proceses)
{
if (proces.Id == processId)
{
proces.Kill();
}
}
But here nothing is killed because no process with the above id is fetched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,运行过程的过程ID在运行时不会更改。
如果没有过程可以使用您启动的过程ID杀死的过程,则意味着两件事:
No, the process id of a running process does not change while it is running.
If there is no process to kill with the process id of the process you started, it means either of two things:
如果要杀死创建的过程,则应保留过程对象并在其上调用杀伤方法。应该无需通过系统中的所有过程来找到启动过程。例如:
If you want to kill the created process you should keep the process-object and call the kill method on it. There should be no need to go thru all the processes in the system to find the started process. For example: