你好,在我的主窗体中,我正在运行一个命令行exe来为我做一些事情。唯一的问题是,因为它正在做它需要做的事情,所以我的父窗体完全冻结,直到这个过程结束。
有没有办法在不冻结父窗体的情况下运行该过程,以便可以单击按钮,等等?谢谢,我已经在下面提供了运行该进程的代码。
Process newprocess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = @"C:\tool.exe";
startInfo.Arguments = "Some Arguments";
newprocess.StartInfo = startInfo;
newprocess.Start();
newprocess.WaitForExit();
发布于 2018-08-13 05:32:45
将问题中的代码放入一个名为RunProcessAndWait()
的方法中,然后在事件处理程序中调用Task.Run(() => RunProcessAndWait());
如果您需要在事件中执行UI操作,请在流程完成后使用async:
private async void OnSomethingPressed(...)
{
await Task.Run(() => RunProcessAndWait());
// Do UI (or other) things here
}
如果你想让等待变得更加异步-这里可能不需要,但为了完整性,它将为你节省一段时间- SO truly referred to in the comments提供了一个答案,展示了如何做到这一点。
https://stackoverflow.com/questions/51812627
复制相似问题