Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Process.Start()导致应用程序挂起_C#_.net_Console Application - Fatal编程技术网

C# Process.Start()导致应用程序挂起

C# Process.Start()导致应用程序挂起,c#,.net,console-application,C#,.net,Console Application,我在Process.Start()方面遇到了困难 代码如下: static void Main(string[] args) { string fileName = @"C:\path_to_project\bin\Debug\helloworld.exe"; ProcessStartInfo info = new ProcessStartInfo(fileName); info.UseShellExecute = false; info.RedirectSta

我在
Process.Start()方面遇到了困难
代码如下:

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();
}
代码应该只调用helloworld控制台应用程序,该应用程序只有一行
console.WriteLine(“Hello World”)
,没有什么特别的

当我调试这段代码时,我到达
Process=Process.Start(info)然后当我点击“跨步”(F10)时,什么也没有发生。我的应用程序挂起,我启动的进程在那里,但它没有结束。我要把他们都杀了

这段代码在我同事的机器上运行,但在我的机器上进程挂起,我唯一能做的就是终止进程

我还注意到,当我双击资源管理器中的任何控制台应用程序时,光标会将状态更改为“忙”,并且在我杀死explorer.exe之前不会再更改


可能是安全问题还是恶意软件?

所以实际上你的代码看起来不错。它确实适用于我的一个简单的“Hello World!”控制台应用程序

然而,我记得不久前我们遇到了这样的问题,在我们试图从中获取输出的进程上挂起了
进程。WaitForExit()

不幸的是,我不记得这是否与你在这里遇到的问题完全相同。但我建议尝试另一种可能性来实现你的目标:

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();
}

很抱歉,我无法解释为什么您的代码挂起(正如我所说,它对我有效)。但是这个变体可能会起作用。

Avast Antivirus拦截Process.Start(),可能它试图对我试图启动的应用程序进行沙箱处理,使其挂起

我暂时禁用了shield,现在可以工作了


感谢大家的努力。

如果您有helloworld.exe的源代码,请尝试在发布模式下重新构建它,以避免调试。由于将调试器附加到解决方案,应用程序可能会挂起。另外,尝试从创建流程的位置运行解决方案,而不进行调试(CTRL+F5)然后看看会发生什么。不应该将
CreateNoWindow
设置为false吗?在发布模式下也是一样,不需要调试就可以开始。我不想让它只显示控制台中打印的文本。试试这个:通过单击控制台窗口左上角的图标转到菜单。转到“选项”选项卡上的“属性”,在“编辑选项”下,取消选中“快速编辑模式”,单击“确定”,并确认要将更改应用于所有标题相同的窗口。这是Avast Antivirus的问题。@user3522638您的意思是什么?实际上,您和我的代码都应该适用于任何及时退出的进程。如果Avast杀毒程序没有退出,那就可以解释你的问题了。但你说过“Hello World!”应用程序也会发生这种情况。所有控制台应用程序都会发生这种情况。我打开了avast盾牌,它现在可以工作了