0

I have a GUI application that I'm writing in C#, and I need to run a Python script when a button is clicked. The Python script is exiting due to an error, and the window that appears closes immediately so I'm unable to see where the error is happening. Is there any way to prevent the window from closing?

I've seen some other suggestions such as using WaitForExit(), but that still hasn't worked. I've found some answers that have provided suggestions for running a command prompt from C#. This is what I'm currently trying:

private void buttonClick(object sender, EventArgs e)
{
    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = "python.exe";
    p.StartInfo.Arguments = "C:\\Path\\To\\Python\\Main.py";
    p.Start();
    p.WaitForExit();
}

I'd expect p.WaitForExit() to prevent the python window from closing, but clearly the error within the python program is forcing it closed.

Carleen
  • 48
  • 7
  • 1
    *...prevent the python window from closing..* you can't (python.exe is the process which open the window and it's end when error appear) ... but you can read process output and show it ... feel free to find the way how to capture the output in the internet – Selvin Mar 28 '19 at 15:11
  • 1
    ... of course you may start own console with `cmd.exe /k python.exe Main.py` – Selvin Mar 28 '19 at 15:16
  • In the Python program, add `import traceback` at the top. Then put a `try...except` around all the code in your main function. In the `except` clause put `traceback.print_exc(); input()`. That will put a traceback on the screen and wait for you to press Enter. – BoarGules Mar 28 '19 at 15:52
  • Thanks for the suggestion of reading process output and showing it - I ended up following along with this [link](https://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program) and managed to figure out where the error was. – Carleen Mar 28 '19 at 16:46

0 Answers0