C# - 如何检查进程是否成功启动 [英] C# - how to check if a process was successfully started
问题描述
可能的重复:
如何知道 Process.Start() 是否成功?
我的程序中有一个类似于看门狗的进程(我们称之为WD
),它是另一个正在运行的进程(我们称之为A
).我在某个事件启动 WD
进程,假设按下了一个键,我想用这个进程启动另一个进程,我们称之为 B
.
I have a process similar to a watchdog(let's call it WD
) in my program which is another running process(let's call it A
). I am starting WD
process at a certain event, let's say a key is pressed and I want to start another process with this process, let's call it B
.
问题是我想在我知道进程 B
成功启动后关闭初始进程 A
.我该如何检查?
The thing is that I want to shut down the initial process A
after what I know that process B
was successfully started. How can I check that?
我正在使用 Process.Start(argList)
和 ProcessInfo(argList)
开始处理 WD
和 B
句法.
I am starting process WD
and B
with Process.Start(argList)
and ProcessInfo(argList)
syntax.
每个进程都是一个简单的 C# 控制台应用程序.
Each process is a simple C# Console Application.
推荐答案
Process.Start 返回一个布尔值(true 如果进程正确启动)检查此 MSDN link 了解 Process.Start() 方法.
Process.Start returns a boolean (true if process started correctly) Check this MSDN link for Process.Start() method.
您的代码应该类似于:
Process B= new Process();
try
{
B.StartInfo.UseShellExecute = false;
B.StartInfo.FileName = "C:\\B.exe";
B.StartInfo.CreateNoWindow = true;
if (B.Start())
{
// Kill process A
}
else
{
// Handle incorrect start of process B and do NOT stop A
}
}
catch (Exception e)
{
// Handle exception and do NOT stop A
}
这篇关于C# - 如何检查进程是否成功启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!