2

my program is running well all the time but it suddenly stuck at Process.WaitForExit(); without proceeding to the next command. I have already ran the program multiple time without having error. Is anyone one know how can I debug this or is anyone know what is the problem behind? Could it be problem with my python.exe? Thanks!

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);
        }
Traveller
  • 25
  • 4

1 Answers1

1

Your program doesn't print anything until the process exits. If the process never exits, you won't ever see anything, and the problem will be impossible to debug.

Better to print as it goes along

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

see How to keeps colours from msbuild output?

Community
  • 1
  • 1
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465