运行过程时更改过程ID

发布于 2025-01-31 10:30:12 字数 1036 浏览 5 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2025-02-07 10:30:12

不,运行过程的过程ID在运行时不会更改。

如果没有过程可以使用您启动的过程ID杀死的过程,则意味着两件事:

  • 在获得过程列表之前,该过程已经退出。
  • 该过程的名称不是“ unitoolapiserver ”。

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:

  • The process has already exited before you obtain the process list.
  • The name of the process is not "UNIToolAPIServer".
青丝拂面 2025-02-07 10:30:12

如果要杀死创建的过程,则应保留过程对象并在其上调用杀伤方法。应该无需通过系统中的所有过程来找到启动过程。例如:

public class MyProcess{
  private Process myProcess;
  ...
  public void Start(){
    myProcess = new Process();
    var processStartInfo2 = new ProcessStartInfo(
                            unitoolLauncherExePath,
                            "options --port " + port);

    myProcess.StartInfo = processStartInfo2;
    myProcess.Start();
  }
  public void Kill(){
    if(myProcess != null && !myProcess.HasExited){
          myProcess.Kill();
     }
  }
}
   

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:

public class MyProcess{
  private Process myProcess;
  ...
  public void Start(){
    myProcess = new Process();
    var processStartInfo2 = new ProcessStartInfo(
                            unitoolLauncherExePath,
                            "options --port " + port);

    myProcess.StartInfo = processStartInfo2;
    myProcess.Start();
  }
  public void Kill(){
    if(myProcess != null && !myProcess.HasExited){
          myProcess.Kill();
     }
  }
}
   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文