PandaNL
PandaNL

Reputation: 848

VB.NET Getting live status from other application

With my application i call an external commandline tool that converse iso's to other fileformats. Right now i just call the iso converter and it will run in the background, when you would run the iso converter via commandline you see what it is doing, but in my application it just runs in the background.

Right now it only gives me the status after the isoconverter is done in a textbox, how could i change this so i can see live status? like i would see in the command line tool?

This is the code that i'm calling to execute the isoconverter.

Private Sub GETCMD3()
    Dim CMDprocess As New Process
    Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    StartInfo.FileName = "cmd"
    StartInfo.CreateNoWindow = True
    StartInfo.RedirectStandardInput = True
    StartInfo.RedirectStandardOutput = True
    StartInfo.UseShellExecute = False
    CMDprocess.StartInfo = StartInfo
    CMDprocess.Start()
    Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput
    Dim SW As System.IO.StreamWriter = CMDprocess.StandardInput
    SW.WriteLine("Isoconvert.exe " & txtIsoFile.Text)
    SW.WriteLine("exit")
    txtIsoOutput.Text = SR.ReadToEnd
    SW.Close()
    SR.Close()
End Sub

Upvotes: 1

Views: 3073

Answers (3)

jlvaquero
jlvaquero

Reputation: 8785

[Offtopic] I was reviewing your code and maybe I've found a better aproach in the way you can launch Isoconvert.exe.

If I'm not wrong, you can launch Isoconvert.exe using startinfo, without the needed of launch a console command.

Dim CMDprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "Isoconvert.exe" 
StartInfo.Arguments = txtIsoFile.Text
CMDprocess.StartInfo = StartInfo
CMDprocess.Start()

I think you can still read and write stdin and stdout.

Upvotes: 1

Justin
Justin

Reputation: 6711

The problem with your current code is the line

txtIsoOutput.Text = SR.ReadToEnd

This is reading the standard output stream of the command until it is complete. Once it is complete, it assigns the result to your text box.

What you want instead is to read from the StreamReader a little at a time, using StreamReader.ReadLine or perhaps ReadBlock.

Something like:

Dim line as String
Do
    line = SR.ReadLine()
    If Not (line Is Nothing) Then
        txtIsoOutput.Text = txtIsoOutput.Text + line + Environment.NewLine
    End If
Loop Until line Is Nothing

This probably won't be good enough, though. The user interface thread is now busy processing the command output, so the TextBox does not get a chance to update its display. The easiest way to fix this is by adding Application.DoEvents() after modifying the text. Make sure to disable any buttons/menus that call GETCMD3 when GETCMD3 starts, though.

Upvotes: 2

jlvaquero
jlvaquero

Reputation: 8785

I'm not sure, maybe accessing process threads and checking the status?

Something like this:

CMDprocess.Threads(0).ThreadState = ThreadState.Running

Upvotes: 0

Related Questions