Problem with ReadToEnd and output freeze
Hello, I'm trying to embed command prompt to my app. The output textbox is freezing when I run the program. Actually it isn't freezing, it's waiting exe to finish its work. I want to get lines immediately as it does in command prompt.
vb.net Code:
Private Sub CMDAutomate()
Dim cmdProcess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "anexecutable" 'starts cmd window
StartInfo.Arguments = "somearguments"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
cmdProcess.StartInfo = StartInfo
cmdProcess.Start()
Dim SR As System.IO.StreamReader = cmdProcess.StandardOutput
Dim SW As System.IO.StreamWriter = cmdProcess.StandardInput
SW.WriteLine(OutputTextBox.Text) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
By the way, (I don't know where to ask) how can I wrap my codes in this site with vb.net style?
Re: Problem with ReadToEnd and output freeze
ReadToEnd won't return until there's an end to read to. If you want to read the output in blocks then call some other method, e.g. ReadLine or Read.
Quote:
Originally Posted by
nikel
By the way, (I don't know where to ask) how can I wrap my codes in this site with vb.net style?
When you post code snippets inside highlight tags, you get to specify an option for formatting the highlighting. Not surprisingly, if you want VB.NET syntax highlighting then the option should be "vb.net", i.e.
[HIGHLIGHT=vb.net]your code here[/HIGHLIGHT]
Re: Problem with ReadToEnd and output freeze
Hello, thank you for your reply. This works fine:
vb.net Code:
Results = Results & SR.ReadLine & ControlChars.NewLine
Results = Results & SR.ReadLine & ControlChars.NewLine
But how can I learn how many lines in that result?
EDIT: I noticed that using readline does the same thing. I also tried ReadLineAsync.
Re: Problem with ReadToEnd and output freeze
Instead of doing it that way call Process.BeginOutputReadLine and then create an event handler for the OutputDataReceived event.