How to stream windows console's output to a listbox?
Hi, I'm very new to VB.NET.
I'm trying to capture the text shown in a batch file when executed, and show it in a listbox in real time. I Have tested many codes but none of them worked.
I think it's not very complicated, but I don't know the code I should use.
Any help would be appreciated.
Thanks in advance.
Re: How to stream windows console's output to a listbox?
If you don't know the code then how do you know whether it's complicated or not? Also, if you have already tried code then it may well be that you are just one small change away from working code, so it would make sense for you to show us what you did and tell us what happened when you did it. That way we won't waste our time going over things that you already have correct.
Anyway, you might want to check out this thread from our own VB.NET CodeBank forum:
http://www.vbforums.com/showthread.p...tion-2003-2005
Re: How to stream windows console's output to a listbox?
Quote:
Originally Posted by
jmcilhinney
If you don't know the code then how do you know whether it's complicated or not? Also, if you have already tried code then it may well be that you are just one small change away from working code, so it would make sense for you to show us what you did and tell us what happened when you did it. That way we won't waste our time going over things that you already have correct.
Anyway, you might want to check out this thread from our own VB.NET CodeBank forum:
http://www.vbforums.com/showthread.p...tion-2003-2005
I guessed the code was not complicated due to the fact of all the codes I found were very simple.
Anyway, thanks for your recommendations, I'll keep them in mind for my next posts.
Now, respecting to the stream, this is how my code looks:
Code:
Dim process As New Process
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo.CreateNoWindow = True
process.StartInfo.FileName = (FolderBrowserDialog1.SelectedPath + "Encoding.bat")
Application.DoEvents()
process.Start()
Do Until process.HasExited
Application.DoEvents()
System.Threading.Thread.Sleep(250)
Loop
TextBox6.Text = process.StandardOutput.ReadToEnd()
While (process.HasExited = False)
Dim sLine As String = process.StandardOutput.ReadLine
If (Not String.IsNullOrEmpty(sLine)) Then
End If
Me.TextBox6.Text &= sLine & vbCrLf
Application.DoEvents()
End While
It saves the console's text in a textbox, but not it in realtime; it has to wait to the console to finish.
Is there any way to make it work in real time?
Thanks.
Re: How to stream windows console's output to a listbox?
You've got an extremely horrible loop in there that keeps doing nothing until the process exits and then you read the entire contents of the stream. If you don't want to wait until the process has exited to read the contents then don't wait until the process has exited to read the contents.
Your code really doesn't make sense. You've got one loop that keeps going until the process has exited and then you've got another loop that keeps going while the loop hasn't exited. If the process has to exit in order to break out of the first loop then how can the second loop possibly do anything useful? As I say to many beginners, try picking up a pen and paper and writing down exactly what you want to code do before writing the code to do it. You don't need any programming experience to know what you want the code to do so even a rank beginner can do that. You can then check whether the code you write actually does what you want. If you had done that then it would be obvious that the code you have written doesn't do what you want.
Re: How to stream windows console's output to a listbox?
Quote:
Originally Posted by
jmcilhinney
You've got an extremely horrible loop in there that keeps doing nothing until the process exits and then you read the entire contents of the stream. If you don't want to wait until the process has exited to read the contents then don't wait until the process has exited to read the contents.
Your code really doesn't make sense. You've got one loop that keeps going until the process has exited and then you've got another loop that keeps going while the loop hasn't exited. If the process has to exit in order to break out of the first loop then how can the second loop possibly do anything useful? As I say to many beginners, try picking up a pen and paper and writing down exactly what you want to code do before writing the code to do it. You don't need any programming experience to know what you want the code to do so even a rank beginner can do that. You can then check whether the code you write actually does what you want. If you had done that then it would be obvious that the code you have written doesn't do what you want.
What do you mean by "then don't wait until the process has exited to read the contents." That's what I'm trying to do, but the code doesn't work. The loop is because the form freezes when the process is started, so it forces the form to be active, otherwise I wouldn't use it.
My intend is: Start a batch file and show the console's text in a textbox in real time. That's what I want my code to do.
Re: How to stream windows console's output to a listbox?
If I asked you to write down what you wanted a car to do would you say that you wanted a metal box to take you from one place to another? Of course not, because that lacks the detail required to be useful, as does your description of what you want your code to do. If that's as detailed as you can describe the process then how can you possibly hope to write code to do it?
As for that first loop, have you actually thought about what it's doing? It says "until the process exits, process user input and then wait 250 milliseconds". If that's all you are going to do until the process exits then how can you possibly read output from the process before it exits?
No matter what anyone says, NEVER, EVER put Application.DoEvents in a loop. If you want to execute some code while maintaining a responsive UI then you execute that code on a secondary thread. The simplest way to do that is using a BackgroundWorker. Call RunWorkerAsync and then handle the DoWork event. That event handler is executed on a secondary thread, so any code in there will not block the UI thread. Any time you need to update the UI, call ReportProgress and handle the ProgressChanged event. That event handler is executed on the UI thread so any code in there can safely access the UI. For examples, follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker.
Re: How to stream windows console's output to a listbox?
Well, after learning to use Background Worker, I've got this code:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim start_info As New ProcessStartInfo()
start_info.FileName = ("cmd.exe")
start_info.UseShellExecute = False
start_info.CreateNoWindow = False
start_info.RedirectStandardOutput = True
start_info.WindowStyle = ProcessWindowStyle.Hidden
start_info.Arguments = (FolderBrowserDialog1.SelectedPath + "\Encoding.bat")
Dim proc As New Process
proc.StartInfo = start_info
proc.Start()
Dim std_out As StreamReader = proc.StandardOutput
Do
TextBox5.Text += std_out.ReadLine
Loop While proc.HasExited = False
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Label6.Text = "Success!"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
It's working fine, the TextBox5 actually displays the console's text.
Now, I have a little problem, and it's that "Arguments" seems not to be working. It's supposed to open command prompt and then open the batch file. But the TextBox5 only shows windows console's default message, which means that it's not passing arguments.