[英]Process.Start() causing application to hang
I'm having a hard time with Process.Start()
我在使用Process.Start()
时遇到困难
Here is the code: 这是代码:
static void Main(string[] args)
{
string fileName = @"C:\path_to_project\bin\Debug\helloworld.exe";
ProcessStartInfo info = new ProcessStartInfo(fileName);
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;
Process process = Process.Start(info);
string error = process.StandardError.ReadToEnd();
string returnvalue = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.ReadLine();
}
The code should only call helloworld console application which has only one line Console.WriteLine("Hello World")
, nothing special. 该代码仅应调用helloworld控制台应用程序,该应用程序只有一行Console.WriteLine("Hello World")
,没什么特别的。
When I debug this code I reach the Process process = Process.Start(info);
当我调试此代码时,我到达Process process = Process.Start(info);
Then when I hit Step over (F10) nothing happens. 然后,当我按下“跳过”(F10)时,什么也没有发生。 My application hangs and the process I started is there but it is not finishing. 我的应用程序挂起,开始的过程在那里,但是还没有完成。 I need to kill'em all. 我要全部杀死他们。
This code works on my colleague's machine but on my machine the process just hangs and only thing I can do is kill the process. 这段代码可以在我同事的机器上工作,但是在我的机器上该过程只是挂起,我唯一能做的就是杀死该过程。
I also noticed that when I double click any console application in explorer the cursor changes the state to busy and never changes back until I kill explorer.exe. 我还注意到,当我双击资源管理器中的任何控制台应用程序时,光标会将状态更改为忙碌,并且直到我杀死explorer.exe才恢复原状。
Could it be some security issue or malware? 可能是某些安全问题或恶意软件?
Avast Antivirus intercepts Process.Start(), probably it tries to sandbox the application i'm trying to start so it hangs. Avast Antivirus截获了Process.Start(),可能是它试图将我要启动的应用程序沙盒化,因此挂起了。
I disabled shield temporary and it now works. 我禁用了临时屏蔽,现在可以使用。
Thank you all for the effort. 谢谢大家的努力。
So actually your code looks fine. 因此,实际上您的代码看起来不错。 And it does work for me with a simple "Hello World!" 它通过一个简单的“ Hello World”对我有用。 console application. 控制台应用程序。
However, I remember that we had a problem like this some time ago, with a hanging process.WaitForExit()
on a process we tried to get the output from. 但是,我记得前段时间我们遇到了这样的问题, process.WaitForExit()
挂起了process.WaitForExit()
在试图从中获取输出的进程上。
Unfortunatly I can't remember if it was exactly the same problem as you encounter here. 不幸的是,我不记得这与您在这里遇到的问题是否完全相同。 But I suggest to try another possibility for what you are trying to achieve: 但我建议尝试另一种可能实现的目标:
static void Main(string[] args)
{
string fileName = @"C:\path_to_project\bin\Debug\helloworld.exe";
ProcessStartInfo info = new ProcessStartInfo(fileName);
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;
StringBuilder outputBuilder = new StringBuilder();
StringBuilder errorBuilder = new StringBuilder();
Process process = new Process {StartInfo = info};
process.OutputDataReceived += (sender, e) => outputBuilder.Append(e.Data);
process.ErrorDataReceived += (sender, e) => errorBuilder.Append(e.Data);
process.Start();
process.BeginErrorReadLine(); // do this after process.Start()
process.BeginOutputReadLine();
process.WaitForExit();
string error = errorBuilder.ToString();
string returnvalue = outputBuilder.ToString();
Console.WriteLine("Returned: {0}", returnvalue);
Console.WriteLine("Error: {0}", error);
Console.ReadLine();
}
I'm sorry that I cannot explain why your code is hanging (and as I said it's working for me). 很抱歉,我无法解释您的代码为何挂起(正如我所说的那样对我有用)。 But maybe this variation will work. 但是也许这种变化会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.