0

Hi I know there is alot of the same question out here already but I tried every single one of them and can't seem to get why it won't work.

Here is my code

    Private Sub CallbackProcessAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)


        Console.WriteLine(args.Data)
        Me.Invoke(Sub() statusRichText.AppendText(args.Data & Environment.NewLine))

    End Sub




Sub SuperUpload()

    Dim oProcess As New Process()

    AddHandler oProcess.ErrorDataReceived, AddressOf CallbackProcessAsync
    AddHandler oProcess.OutputDataReceived, AddressOf CallbackProcessAsync

    Dim oStartInfo As New ProcessStartInfo("C:\Users\RKjetski\AppData\Local\Programs\Python\Python37\python.exe", "test.py " + vInfoIframe.Text + " " + vInfoID.Text)
    oStartInfo.UseShellExecute = False
    oStartInfo.CreateNoWindow = True
    oStartInfo.RedirectStandardError = True
    oStartInfo.RedirectStandardOutput = True


    oProcess.EnableRaisingEvents = True
    oProcess.StartInfo = oStartInfo
    oProcess.Start()

    oProcess.BeginErrorReadLine()
    oProcess.BeginOutputReadLine()

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSuperUpload.Click


    Dim thread = New System.Threading.Thread(AddressOf SuperUpload)
    thread.Start()
End Sub

The python file

    import time
x = 0
while x < 5:
    print(x)
    time.sleep(2)
    x = x +1

I get the following output but it's not live / in real time, the rich text box is empty until it read the program and then it prints everything instantly. 0 1 2 3 4

Smixen
  • 51
  • 8
  • Remove the thread. You're Process is already event-driven at this point (already *asynchronous*). Use two different handlers for `OutputDataReceived` and `ErrorDataReceived` (easier to determine what's happening). Use BeginInvoke() with a [MethodInvoker](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.methodinvoker) Delegate (e.g., `BeginInvoke(New MethodInvoker(Sub() statusRichText.AppendText(...) End Sub)))`. [EnableRaisingEvents](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.enableraisingevents) is only used to raise the `Exited` event (...). – Jimi Oct 21 '19 at 17:45
  • (...) The asynchronous version of [Process.WaitForExit](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit). Add a handler to the `Exited` event (where you also dispose of the `Process` object) or use `WaitForExit` (here, use the former). You can follow this pattern: [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) (C# code, but follow the notes there. The code is quite simple, though) – Jimi Oct 21 '19 at 17:46
  • Still didn't get it to work, seems to be the same. If I add a 1 sec paus on OutputDataRecived then it prints out each line one by one so I don't get it why it still has to wait to load the whole script? – Smixen Oct 22 '19 at 15:39
  • https://pastebin.com/G0GYx3vG Posted the updated code here – Smixen Oct 22 '19 at 15:40
  • Just noticed it seems to work on my error handler when ffmpeg is running it's script? Not sure if it's suppose to output as error tho? – Smixen Oct 23 '19 at 23:33

0 Answers0