我的程序停留在Process.WaitForExit();

时间:2013-07-16 10:46:55

标签: c# python wpf

我的程序一直运行良好但它突然停留在Process.WaitForExit();无需继续执行下一个命令。我已经多次运行该程序而没有错误。有谁知道如何调试这个或者有谁知道背后的问题是什么?我的python.exe可能有问题吗?谢谢!

public static void Process(string location)
    {
        int ExitCode;
        ProcessStartInfo ProcessInfo;
        Process Process;

        ProcessInfo = new ProcessStartInfo();
        ProcessInfo.FileName = location;
        ProcessInfo.Arguments = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + 
            "\\parse.py " + Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\cache.cell";
        ProcessInfo.CreateNoWindow = true;
        ProcessInfo.UseShellExecute = false;
        ProcessInfo.RedirectStandardOutput = true;
        ProcessInfo.RedirectStandardError = true;
        Process = Process.Start(ProcessInfo);
        // (...)
        Process.WaitForExit();

        string stderr = Process.StandardError.ReadToEnd();
        string stdout = Process.StandardOutput.ReadToEnd();
        //Console.WriteLine("STDERR: " + stderr);
        //Console.WriteLine("STDOUT: " + stdout);

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test.txt"))
        {
            file.WriteLine(stdout);
        }

1 个答案:

答案 0 :(得分:1)

在程序退出之前,您的程序不会打印任何内容。如果进程永不退出,您将永远不会看到任何内容,并且无法调试该问题。

最好随时打印

var info = new ProcessStartInfo("msbuild")
{
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true,            
};

using (var p = Process.Start(info) )
{
    p.ErrorDataReceived += (s, e) => Console.Error.WriteLine(e.Data);
    p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
    p.BeginErrorReadLine();
    p.BeginOutputReadLine();
    p.WaitForExit();
}

请参阅How to keeps colours from msbuild output?