0

I am trying to invoke the following .py script: (python version installed: 3.11.4)

TaskName= input("Kindly Enter which task should be executed, for Eg: A1,B1,C1 : ")
print(TaskName)

from the below simple C# code:

string path = @"C:\Program Files\Python311\python.exe";
                string input = @"C:\app\new_7.py";

                ProcessStartInfo startInfo = new ProcessStartInfo();                    
                startInfo.FileName = path;
                startInfo.Arguments = "\"" + input + "\"";
                startInfo.UseShellExecute = false;                    
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;
                using (Process exeProcess = Process.Start(startInfo))
                {
                    string line = exeProcess.StandardOutput.ReadToEnd();
                    exeProcess.WaitForExit();
                    if (exeProcess.ExitCode == 0) MessageBox.Show("External program is executed successfully.");
                    else MessageBox.Show("External program execution was not successful.");
                    line = string.IsNullOrEmpty(line) ? line : line.TrimEnd(new char[] { '\r', '\n' });

                    MessageBox.Show(line);
                }

But, it doesn't show-up the prompt what i see when i run the script by double clicking the .py (screenshot below). When debugged, i see this code: using (Process exeProcess = Process.Start(startInfo)) passes without showing the prompt and gets stuck at next line: string line = exeProcess.StandardOutput.ReadToEnd();

enter image description here

Can anyone give some hint on where i am going wrong? I don't want new window that's why : startInfo.CreateNoWindow = true;. If i set it to False: it flashes the prompt and goes immediately.

I have seen the similar issue asked in this: Run Python script in c# does nothing

But the solution doesn't work for me.

7
  • You can't both redirect the output and not redirect the output - do you want your C# app to consume output or do you want the console user to? Commented Feb 20, 2024 at 10:38
  • @MathiasR.Jessen I want my C# app to consume the output and take action further. Commented Feb 20, 2024 at 11:25
  • 1
    Alright, then avoid StandardOutput.ReadToEnd() Commented Feb 20, 2024 at 11:29
  • 1
    okay, thanks very much for the link. now i understood what you were asking actually. What i basically want is to show the prompt from my .py script to the user (meaning redirect output for the user) and then to get the input given by user to my C# app (redirect to my c# app). So, with the solution given in stackoverflow.com/a/7608823/5658446, i could get the output to my c# app (not getting stuck anymore), but missed to show the prompt (screenshot above) to the user so that he can enter his input. Commented Feb 21, 2024 at 7:04
  • 1
    Gotcha! Yeah, the straightforward solution here is to just read standard output line-by-line and then echo it from the UI of your C# app in realtime as you process it :) Commented Feb 21, 2024 at 10:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.