c# - 为什么 Process.WaitForExit 不等待?

标签 c# process

为什么 Process.WaitForExit 不等待?

static void Main(string[] args)
    {
        while (true)
        {
            try
            {
                if (Process.GetProcessesByName("ServerAPPFileManager").Length > 0)
                {
                    Process clsProcess = Process.GetProcessesByName("ServerAPPFileManager")[0];
                    Console.WriteLine(clsProcess.ProcessName);
                    clsProcess.WaitForExit();
                }
                if (System.IO.File.Exists(System.IO.Path.Combine(Environment.CurrentDirectory, "ServerAPPFileManager.exe")))
                {
                    Console.WriteLine("File Found");
                    Process p = new Process();
                    p.StartInfo.Verb = "runas";
                    p.StartInfo.FileName = System.IO.Path.Combine(Environment.CurrentDirectory, "ServerAPPFileManager.exe");
                    p.Start();
                }
            }
            catch(Exception e)
            {
                Console.Write(e.Message);
                System.Threading.Thread.Sleep(1000);
            }
        }
    }

它不断循环并一次又一次地启动应用程序!

编辑:

它现在起作用了,它不起作用,因为它就像这样 clsProcess.WaitForExit(1000);

最佳答案

此代码包含在 while(true) 循环中,并且没有 breakthrowreturn > 循环体内的语句。这确实会导致无限循环。

如果您想在 WaitForExit 完成后退出循环,则需要引入 break 语句来执行此操作。

clsProcess.WaitForExit();
break;

关于c# - 为什么 Process.WaitForExit 不等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10125607/

相关文章:

c# - 如何使用linq通过主键组合三个对象列表

c# - MVVM 中公共(public)属性的实践

c# - 我想要一个字符转义 IsLetterOrDigit

linux - 进程 sighandler 可以处理 linux 信号 SIG_KILL 吗?

c++ - 使用 IPC 共享内存的应用程序可以访问彼此的代码吗?

c - 如何使用 C 中的信号将多个值从子进程传递到父进程?

c# - 异步生产者/消费者

c# - 循环中从 CryptoStream 获取字符串

java - 如何通过 TCP 套接字连接重定向进程的输入和输出? ( java )

linux - 如何在 Unix/Linux 中控制调度程序启动的进程