0

I have a process which runs python in the background in order to receive the inputs and outputs. My program works with multiple outputs but only allows one input otherwise the program freezes. If the python.exe wants different values for the new user inputs how will I do this?

VB.net

Public Value As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Works on button
        Dim p As Process = New Process
        p.StartInfo.FileName = "C:\Users\luqma\AppData\Local\Programs\Python\Python38-32\python.exe" 'Default Python Installation
        p.StartInfo.Arguments = "File.py"
        p.StartInfo.UseShellExecute = False 'required for redirect.
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 'don't show commandprompt.
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardInput = True
        p.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
        p.StartInfo.RedirectStandardError = True
        p.Start()

        AddHandler p.OutputDataReceived, AddressOf process_OutputDataReceived
        p.StandardInput.WriteLine(TextBox2.Text)
        p.StandardInput.WriteLine(TextBox2.Text) 'Freezes with this input added. Only works with one use input

        p.BeginOutputReadLine()

        SimpleEditor1.Text = Value
        p.WaitForExit()
    End Sub
    Public Sub process_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        On Error Resume Next
        If e.Data = "" Then
        Else
            Value += ">" + e.Data + Environment.NewLine
        End If
    End Sub

Python code I'm trying to do

print("bye")
print("Hello") 
i = int(input("Enter num:"))#RUNS WHEN THERE IS ONLY THIS INPUT
print(i+1)
q = int(input("Enter num:"))#FREEZES WHEN THIS IS IN THE PYTHON FILE
print(q+5)

Thanks for the assistance

Luqmaan
  • 13
  • 6
  • Read the notes here: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). See how StdIn is redirected to a Stream and how command are received and sent. Also, you cannot assume that setting this: `SimpleEditor1.Text = Value` and then changing the `Value` string would do anything: you have to append text to the Control. See how the Exited event is handled, so you ca remove that `p.WaitForExit()`, close the Process when you don't need it anymore and also `Dispose()` of it. – Jimi Oct 11 '20 at 15:10
  • That's a test with a CMD Console. With a different console app it can be slightly different, there are some notes in code. Note that there's a sample Form that can be downloaded for testing. Try to write the content of `TextBox2.Text` when you receive the `Enter num:` prompt from StdOut. – Jimi Oct 11 '20 at 15:11

0 Answers0