|
-
May 21st, 2015, 03:25 AM
#1
Thread Starter
Addicted Member
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?
Last edited by nikel; May 21st, 2015 at 05:33 AM.
Reason: I edited highlight
-
May 21st, 2015, 05:04 AM
#2
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.
 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]
-
May 21st, 2015, 06:33 AM
#3
Thread Starter
Addicted Member
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.
Last edited by nikel; May 21st, 2015 at 07:01 AM.
Reason: I edited the post
-
May 21st, 2015, 06:51 AM
#4
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|