C# - 使 Process.Start 等待进程启动 [英] C# - Making a Process.Start wait until the process has start-up

查看:35
本文介绍了C# - 使 Process.Start 等待进程启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继续使用方法之前,我需要确保进程正在运行.

I need to make sure that a process is running before moving on with a method.

声明如下:

Process.Start("popup.exe");

您可以执行 WAIT 命令或对此值设置延迟吗?

Can you do a WAIT command or set a delay on this value?

推荐答案

你的意思是等到它完成吗?然后使用 Process.WaitForExit:

Do you mean wait until it's done? Then use Process.WaitForExit:

var process = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "popup.exe"
    }
};
process.Start();
process.WaitForExit();

或者,如果您正在等待进入消息循环的具有 UI 的应用程序,您可以说:

Alternatively, if it's an application with a UI that you are waiting to enter into a message loop, you can say:

process.Start();
process.WaitForInputIdle();

最后,如果这些都不适用,只需 Thread.Sleep 一段合理的时间:

Lastly, if neither of these apply, just Thread.Sleep for some reasonable amount of time:

process.Start();
Thread.Sleep(1000); // sleep for one second

这篇关于C# - 使 Process.Start 等待进程启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆