|
-
May 25th, 2010, 04:24 PM
#1
Thread Starter
Lively Member
Running a DOS command, and show output in textbox, realtime?
I want to run some dos commands (eg: a ROBOCOPY) and show the output from this realtime in a text window to show the user something is happening...
The command may take a number of minutes so it's important the output is showing in the VB application realtime, and not just all at the end. Obviously the user will wonder what is happening during this time.
Is there an easy way to achieve this?
ps: I've only been using Visual Basic 2010 for a couple of weeks, so go easy on me please
-
May 25th, 2010, 04:26 PM
#2
Re: Running a DOS command, and show output in textbox, realtime?
There's a code example to do exactly this, in the CodeBank
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
May 25th, 2010, 04:36 PM
#3
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by weirddemon
There's a code example to do exactly this, in the CodeBank
Any chance of a link?
If this example - http://www.vbforums.com/showthread.p...&highlight=dos
Then that's the sort of thing I've tried so far, but only updates the text in the VB application once the dos command has completed, not during the running of the dos command.
-
May 25th, 2010, 04:50 PM
#4
Frenzied Member
Re: Running a DOS command, and show output in textbox, realtime?
What about piping the output to a text file then reading it every so often?
-
May 25th, 2010, 06:14 PM
#5
Re: Running a DOS command, and show output in textbox, realtime?
Are you saying that your dos command will do something like:
running....
Info on screen
running
Info on screen
Something like this?
I don't know if you can step withing the execution of an exe file.Maybe getting a pointer on some address of exe but it's gonna get messy.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 26th, 2010, 01:20 AM
#6
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by BrianS
What about piping the output to a text file then reading it every so often?
That sounds like a possibility. So the only hurdles there are:-
1) Kicking the DOS command off and not just waiting for it to finish, but instead entering a loop in VB, say polling every 5 seconds, until it finishes.
2) In the loop picking up the txt output (every 5 seconds) and displaying it in a text box.
(2) Sound easy (even for me) 
(1) I believe the VB function I have to initiate the DOS command says whether to wait for it to finish or not, but I'm not sure how I'd monitor if it's still in existance or not (ie: has it finished)? - ie: Do I stay in my polling loop or not?
Additionally, if I want to 'abort' the run, the command I'd need to terminate the DOS process!?
Thanks for the help guys!
-
May 26th, 2010, 01:23 AM
#7
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by sapator
Are you saying that your dos command will do something like:
running....
Info on screen
running
Info on screen
Something like this?
I don't know if you can step withing the execution of an exe file.Maybe getting a pointer on some address of exe but it's gonna get messy.
Yes, think of a massive ROBOCOPY taking place copying thousands of files. This would put out a line for each file copied... So all I want to do is show this output in the controlling VB application so the user knows something is happening and it's not just hung...
-
May 26th, 2010, 03:34 AM
#8
Re: Running a DOS command, and show output in textbox, realtime?
This is a sample wrapper around cmd.exe that demonstrates how to capture standard output in realtime.
vb Code:
Imports System.Diagnostics
Public Class frmConsoleDemo
Friend WithEvents txtConsoleOut As System.Windows.Forms.TextBox
Friend WithEvents txtConsoleIn As System.Windows.Forms.TextBox
Private psi As ProcessStartInfo
Private cmd As Process
Private Delegate Sub InvokeWithString(ByVal text As String)
Private Sub frmConsoleDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
psi = New ProcessStartInfo("cmd.exe")
Dim systemencoding As System.Text.Encoding = _
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
With psi
.UseShellExecute = False ' Required for redirection
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.StandardOutputEncoding = systemencoding ' Use OEM encoding for console applications
.StandardErrorEncoding = systemencoding
End With
' EnableraisingEvents is required for Exited event
cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
AddHandler cmd.Exited, AddressOf CMD_Exited
cmd.Start()
' Start async reading of the redirected streams
' Without these calls the events won't fire
cmd.BeginOutputReadLine()
cmd.BeginErrorReadLine()
Me.txtConsoleIn.Select()
End Sub
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
Me.Close()
End Sub
' This sub gets called in a different thread so invokation is required
Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
End Sub
Private Sub Sync_Output(ByVal text As String)
txtConsoleOut.AppendText(text & Environment.NewLine)
txtConsoleOut.ScrollToCaret()
End Sub
' Sending console commands here
Private Sub txtConsoleIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsoleIn.KeyPress
If e.KeyChar = ControlChars.Cr Then
cmd.StandardInput.WriteLine(txtConsoleIn.Text)
txtConsoleIn.Clear()
End If
End Sub
' Two text boxes called txtConsoleOut and txtConsoleIn
Public Sub New()
Me.txtConsoleOut = New System.Windows.Forms.TextBox()
Me.txtConsoleIn = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'txtConsoleOut
'
Me.txtConsoleOut.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtConsoleOut.Location = New System.Drawing.Point(12, 12)
Me.txtConsoleOut.Multiline = True
Me.txtConsoleOut.Name = "txtConsoleOut"
Me.txtConsoleOut.ReadOnly = True
Me.txtConsoleOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtConsoleOut.Size = New System.Drawing.Size(665, 494)
Me.txtConsoleOut.TabIndex = 0
'
'txtConsoleIn
'
Me.txtConsoleIn.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtConsoleIn.Location = New System.Drawing.Point(10, 512)
Me.txtConsoleIn.Name = "txtConsoleIn"
Me.txtConsoleIn.Size = New System.Drawing.Size(667, 20)
Me.txtConsoleIn.TabIndex = 1
'
'frmConsoleDemo
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(689, 544)
Me.Controls.Add(Me.txtConsoleIn)
Me.Controls.Add(Me.txtConsoleOut)
Me.Name = "frmConsoleDemo"
Me.Text = "Console redirect demo"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
End Class
-
May 26th, 2010, 02:05 PM
#9
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by cicatrix
This is a sample wrapper around cmd.exe that demonstrates how to capture standard output in realtime.
vb Code:
Imports System.Diagnostics Public Class frmConsoleDemo Friend WithEvents txtConsoleOut As System.Windows.Forms.TextBox Friend WithEvents txtConsoleIn As System.Windows.Forms.TextBox Private psi As ProcessStartInfo Private cmd As Process Private Delegate Sub InvokeWithString(ByVal text As String) Private Sub frmConsoleDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load psi = New ProcessStartInfo("cmd.exe") Dim systemencoding As System.Text.Encoding = _ System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage) With psi .UseShellExecute = False ' Required for redirection .RedirectStandardError = True .RedirectStandardOutput = True .RedirectStandardInput = True .CreateNoWindow = True .StandardOutputEncoding = systemencoding ' Use OEM encoding for console applications .StandardErrorEncoding = systemencoding End With ' EnableraisingEvents is required for Exited event cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True} AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received AddHandler cmd.Exited, AddressOf CMD_Exited cmd.Start() ' Start async reading of the redirected streams ' Without these calls the events won't fire cmd.BeginOutputReadLine() cmd.BeginErrorReadLine() Me.txtConsoleIn.Select() End Sub Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs) Me.Close() End Sub ' This sub gets called in a different thread so invokation is required Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs) Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data) End Sub Private Sub Sync_Output(ByVal text As String) txtConsoleOut.AppendText(text & Environment.NewLine) txtConsoleOut.ScrollToCaret() End Sub ' Sending console commands here Private Sub txtConsoleIn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsoleIn.KeyPress If e.KeyChar = ControlChars.Cr Then cmd.StandardInput.WriteLine(txtConsoleIn.Text) txtConsoleIn.Clear() End If End Sub ' Two text boxes called txtConsoleOut and txtConsoleIn Public Sub New() Me.txtConsoleOut = New System.Windows.Forms.TextBox() Me.txtConsoleIn = New System.Windows.Forms.TextBox() Me.SuspendLayout() ' 'txtConsoleOut ' Me.txtConsoleOut.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.txtConsoleOut.Location = New System.Drawing.Point(12, 12) Me.txtConsoleOut.Multiline = True Me.txtConsoleOut.Name = "txtConsoleOut" Me.txtConsoleOut.ReadOnly = True Me.txtConsoleOut.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.txtConsoleOut.Size = New System.Drawing.Size(665, 494) Me.txtConsoleOut.TabIndex = 0 ' 'txtConsoleIn ' Me.txtConsoleIn.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.txtConsoleIn.Location = New System.Drawing.Point(10, 512) Me.txtConsoleIn.Name = "txtConsoleIn" Me.txtConsoleIn.Size = New System.Drawing.Size(667, 20) Me.txtConsoleIn.TabIndex = 1 ' 'frmConsoleDemo ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(689, 544) Me.Controls.Add(Me.txtConsoleIn) Me.Controls.Add(Me.txtConsoleOut) Me.Name = "frmConsoleDemo" Me.Text = "Console redirect demo" Me.ResumeLayout(False) Me.PerformLayout() End Sub End Class
I'm afraid although that works, my knowlegde of VB makes it seemingly impossible to take the code out that I need 
If I paint my form with two big text boxes txtConsoleIn and txtConsoleOut, then the form shown at runtime doesn't resemble that at all and has been completely redrawn 
I've tried playing around with that final Sub New, but can't seem to get the form to work then and get errors 
If it just did a "tracert www.bbc.co.uk" for example (no need for txtConsoleIn), out to txtConsoleOut, that would be a perfect example. Obviously without having to redraw my entire form to do so...
-
May 27th, 2010, 12:55 AM
#10
Re: Running a DOS command, and show output in textbox, realtime?
Actually you don't need to redraw your form.
The txtConsoleIn and txtConsoleOut textboxes are just for example.
If you need only to display the output of a command you a) don't need txtConsoleIn at all and b) this example shows how to receive data from the command's standard output stream (see Async_Data_Received sub). This sub is invoked each time the command sends something to its standard output stream (and e.Data will hold the string it 'displayed'). Having received this event you simply use this text as you see fit (it's in another thread so you should invoke a sub in the UI thread to manipulate your controls - that's why you need to invoke Sync_Output sub and pass the output text to it). In the Sync_Output sub you simply display the received text in the textbox while you can do something different.
Here's how it works.
1. I start a process and assign an EventHandler for its OutputDataReceived event so each time this command sends something to its' StdOut your programm will be notified.
2. If something is received the Async_Data_Received handler is invoked.
3. It invokes a syncronous with the UI Sync_Output and passes the data it has just received to it.
4. In the Sync_Output you can do whatever you want with the text - display it, parse it, etc.
5. To exit this particular app you need to type Exit but if you run some other command it will exit as soon as the execution finishes.
You need to change "cmd.exe" to "tracert" and pass "www.bbc.co.uk" as its argument and then remove all code for txtConsoleIn, that's all.
-
May 27th, 2010, 04:34 AM
#11
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by cicatrix
Actually you don't need to redraw your form.
The txtConsoleIn and txtConsoleOut textboxes are just for example.
If you need only to display the output of a command you a) don't need txtConsoleIn at all and b) this example shows how to receive data from the command's standard output stream (see Async_Data_Received sub). This sub is invoked each time the command sends something to its standard output stream (and e.Data will hold the string it 'displayed'). Having received this event you simply use this text as you see fit (it's in another thread so you should invoke a sub in the UI thread to manipulate your controls - that's why you need to invoke Sync_Output sub and pass the output text to it). In the Sync_Output sub you simply display the received text in the textbox while you can do something different.
Here's how it works.
1. I start a process and assign an EventHandler for its OutputDataReceived event so each time this command sends something to its' StdOut your programm will be notified.
2. If something is received the Async_Data_Received handler is invoked.
3. It invokes a syncronous with the UI Sync_Output and passes the data it has just received to it.
4. In the Sync_Output you can do whatever you want with the text - display it, parse it, etc.
5. To exit this particular app you need to type Exit but if you run some other command it will exit as soon as the execution finishes.
You need to change "cmd.exe" to "tracert" and pass "www.bbc.co.uk" as its argument and then remove all code for txtConsoleIn, that's all.
I understand the principle, but just cannot get the code to work - I've just spent an hour and half kicking the code around, and got no where. 
I literally just want to be able to specify a command. eg: "tracert www.bbc.co.uk" or a ROBOCOPY, for example, and then catch the output, and add it to a text box to show
-
May 27th, 2010, 05:20 AM
#12
Re: Running a DOS command, and show output in textbox, realtime?
In my example change
Code:
psi = New ProcessStartInfo("cmd.exe")
to
Code:
Dim psi As New ProcessStartInfo("tracert", "www.bbc.co.uk")
-
May 27th, 2010, 07:46 AM
#13
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
OK... Making some headway at last!
This is now working! I can paint my form, just with a text box (txtConsoleOut) and a button (btnRun).
The thing I can now not work out is:-
- While the command is running, how do I prevent the form being used? ie: I don't want them to be able to click the button again, while the last/previous process is still running obviously!
- CMD_Exited is not getting run? This is something to do with me changing EnableRaisingEvents to False. If it was set to True then the form would exit at the end of the process for some odd reason!?
- Is the process I start actually closing afterwards? ie: I'm not leaving orphaned things around?
ps: Thanks for your help...
Code:
Imports System.Diagnostics
Public Class frmConsoleDemo
Private psi As ProcessStartInfo
Private cmd As Process
Private Delegate Sub InvokeWithString(ByVal text As String)
Private Sub frmConsoleDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
'Me.Close()
txtConsoleOut.AppendText(Text & "All done!!!")
txtConsoleOut.ScrollToCaret()
End Sub
' This sub gets called in a different thread so invokation is required
Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
End Sub
Private Sub Sync_Output(ByVal text As String)
txtConsoleOut.AppendText(text & Environment.NewLine)
txtConsoleOut.ScrollToCaret()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
psi = New ProcessStartInfo("robocopy", "d:\foldera d:\folderb /MIR /NP")
Dim systemencoding As System.Text.Encoding = _
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
With psi
.UseShellExecute = False ' Required for redirection
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.StandardOutputEncoding = systemencoding ' Use OEM encoding for console applications
.StandardErrorEncoding = systemencoding
End With
' EnableraisingEvents is required for Exited event
cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = False}
AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
AddHandler cmd.Exited, AddressOf CMD_Exited
cmd.Start()
' Start async reading of the redirected streams
' Without these calls the events won't fire
cmd.BeginOutputReadLine()
cmd.BeginErrorReadLine()
End Sub
End Class
Last edited by NeilF; May 27th, 2010 at 08:08 AM.
-
May 27th, 2010, 08:50 AM
#14
Re: Running a DOS command, and show output in textbox, realtime?
In the btnRun_click add this line:
Code:
btnRun.Enabled = False
In order to correctly catch the Exited event Set EnableRaisingEvents back to true
Code:
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
btnRun.Enabled = True ' < --- Add THis
'Me.Close() <- This is what caused the form to close. If you leave this commented the form won't close
txtConsoleOut.AppendText(Text & "All done!!!")
txtConsoleOut.ScrollToCaret()
End Sub
-
May 27th, 2010, 09:40 AM
#15
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by cicatrix
In the btnRun_click add this line:
Code:
btnRun.Enabled = False
In order to correctly catch the Exited event Set EnableRaisingEvents back to true
Code:
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
btnRun.Enabled = True ' < --- Add THis
'Me.Close() <- This is what caused the form to close. If you leave this commented the form won't close
txtConsoleOut.AppendText(Text & "All done!!!")
txtConsoleOut.ScrollToCaret()
End Sub
Sorry, that's not the case, strangely.
That "Me.Close" WAS already commented out (infact I can even remove it). And with ".EnableRaisingEvents = True" the form closes, via the CMD_EXITED sub.
With " .EnableRaisingEvents = False" the form remains open, but CMD_EXITED is not entered.
-
May 27th, 2010, 10:54 AM
#16
Re: Running a DOS command, and show output in textbox, realtime?
The form cannot close without any 'help'.
Set .EnableRaisingEvents = True and put a breakpoint in the cmd_exited sub.
Then step through your code to see which line makes it close.
-
May 27th, 2010, 01:00 PM
#17
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by cicatrix
The form cannot close without any 'help'.
Set .EnableRaisingEvents = True and put a breakpoint in the cmd_exited sub.
Then step through your code to see which line makes it close.
LOL!
It's the code trying to set the text of txtConsoleOut causing VB to seemingly to bomb out! My CMD_EXITED reads:-
Code:
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
txtConsoleOut.Text = "Done"
End Sub
And I can see the error popping up in the logs of "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"
Very odd! Does something still own txtConsoleOut?
If I take that line out of CMD_EXITED (so it does nothing), then all works fine, the form remains up, and I can press the button to run the script over and over...
Last edited by NeilF; May 27th, 2010 at 01:05 PM.
-
May 28th, 2010, 04:11 AM
#18
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
Any code to do with any of the forms properties causes the form to terminate/end...
eg. Even this makes the form end...
Code:
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
btnRun.Text = "Finished"
End Sub
Where as this means the form stays open, and I can press btnRun over and over:-
Code:
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
Dim x As String
x = "Finished"
End Sub
Here's the entire example (needs textbox txtConsoleout, and a button btnRun):-
Code:
Imports System.Diagnostics
Public Class frmConsoleDemo
Private psi As ProcessStartInfo
Private cmd As Process
Private Delegate Sub InvokeWithString(ByVal text As String)
Private Sub frmConsoleDemo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
btnRun.Text = "Finished"
'Dim x As String
'x = "Finished"
End Sub
' This sub gets called in a different thread so invokation is required
Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
End Sub
Private Sub Sync_Output(ByVal text As String)
txtConsoleOut.AppendText(text & Environment.NewLine)
txtConsoleOut.ScrollToCaret()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
btnRun.Text = "Starting!!!"
psi = New ProcessStartInfo("robocopy", "d:\foldera d:\folderb /MIR /NP")
Dim systemencoding As System.Text.Encoding = _
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
With psi
.UseShellExecute = False ' Required for redirection
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.StandardOutputEncoding = systemencoding ' Use OEM encoding for console applications
.StandardErrorEncoding = systemencoding
End With
' EnableraisingEvents is required for Exited event
cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
AddHandler cmd.Exited, AddressOf CMD_Exited
cmd.Start()
' Start async reading of the redirected streams
' Without these calls the events won't fire
cmd.BeginOutputReadLine()
cmd.BeginErrorReadLine()
End Sub
'Option Explicit
'Private Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
End Class
This is doing my head in
-
May 28th, 2010, 02:00 PM
#19
Re: Running a DOS command, and show output in textbox, realtime?
Well from a quick glance at your code I'm guessing your CMD_Exited method is also being run from another thread so you need to handle it in the same way that you handle the Async_Data_Received method (i.e by using Invoke).
-
May 28th, 2010, 02:37 PM
#20
Thread Starter
Lively Member
Re: Running a DOS command, and show output in textbox, realtime?
 Originally Posted by chris128
Well from a quick glance at your code I'm guessing your CMD_Exited method is also being run from another thread so you need to handle it in the same way that you handle the Async_Data_Received method (i.e by using Invoke).
That should tie up perfectly then with the solution to my other problem - http://www.vbforums.com/showthread.php?t=616434
-
Sep 2nd, 2016, 08:15 PM
#21
New Member
Re: Running a DOS command, and show output in textbox, realtime?
One question, I know this is an old thread..The code works, but what I need to do is keep the console reading..I am running a dedicated server for a video game, and the console for that reads out constantly (everything that happens in game is directed to this console) For some reason when I run the servers .exe file it does about 20 lines and then errors with "CTextConsoleWin32:Getline: !GetNumberOfConsoleInputEvents"..I want to be able to keep the thread open so I can send commands via my app. Basically I am creating a map selector app that you can choose what options you want then it will send all of that to the console, and keep the console open since it must be open for the game server to remain online. Example: (scdrs.exe -game -insurgency +map hijacked -diff 5 -yada -yada)..after this initial command the console could be written to via a textbox (Example: Changelevel Skirmish_v1 checkpoint). ... ANY help would be greatly appreciated!!
Last edited by section.echo; Sep 2nd, 2016 at 08:32 PM.
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
|