我有以下代码,可以打开命令窗口(从WPF接口)并在需要大约8-10分钟的地方执行代码:
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = _exePath;
procStartInfo.Arguments = arguments;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
using (Process pr = Process.Start(procStartInfo))
{
pr.WaitForExit();
string result = pr.StandardOutput.ReadToEnd();
string[] split = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int output = 0;
int.TryParse(split[split.Length - 1], out output);
return output;
}
在
Program.cs
我有更新状态(显示操作状态和百分比)的方法:
Console.Title = "Loading ... 5 %";
// do request to server and check status
while(status.InProgress) {
Thread.Sleep(2000); // 2 seconds
status = web.getJsonFromApiServer(url); // {InProgress: "true", Message: "Checking X%";
}
有时进程被挂起,它的标题不再被更新,就像某些东西进入无限循环一样。
如果我使用控制台而不从WPF启动(我的意思是使用命令提示符,然后将location设置为exe path并使用参数运行它),它可以正常工作,没有问题。
为什么会发生这种事?