By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,482 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,482 IT Pros & Developers. It's quick & easy.

How to know external application exit

P: 7
Hi there,

I want to automate something with vb.net and display my output on message box or text box.

I came across another similar forum that matched what I wanted to do and try the method but it will not work for me:
http://bytes.com/topic/visual-basic-net/answers/721260-how-know-when-external-process-has-exited

I am not sure why rather than displaying my output, it close my form when the external program exited/closed.

Expand|Select|Wrap|Line Numbers
  1. Private WithEvents p As Process = New Process()
  2. Private Sub RunProgram()
  3. p.StartInfo.FileName = cm
  4. p.StartInfo.UseShellExecute = False
  5. p.StartInfo.CreateNoWindow = True
  6. p.StartInfo.RedirectStandardOutput = True
  7. p.StartInfo.RedirectStandardInput = True
  8. p.StartInfo.RedirectStandardError = True
  9.  
  10. p.Start()
  11.  
  12. p.StandardInput.WriteLine("dir")
  13. p.StandardInput.WriteLine("help")
  14. End Sub()
  15.  
  16. Private Sub processExited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles winscp.Exited
  17. TextBox3.Text = "Console Output: " & vbCrLf & winscp.StandardOutput.ReadToEnd()
  18. End Sub
Anyone please help.

Thanks.
Apr 22 '13 #1

✓ answered by IronRazer

Hi,
If you want to display the StandardOutput in a messagebox you can do it like this without freezing the program thread. However you may want to filter the output string before displaying it.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim proc As Process
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         StartMyProcess()
  6.     End Sub
  7.  
  8.     Private Sub StartMyProcess()
  9.         proc = New Process
  10.         proc.StartInfo.FileName = "cmd.exe"
  11.         proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  12.         proc.StartInfo.CreateNoWindow = False
  13.         proc.StartInfo.UseShellExecute = False
  14.         proc.StartInfo.RedirectStandardOutput = True
  15.         proc.StartInfo.RedirectStandardInput = True
  16.  
  17.         'Added these two lines of code to enable the process to raise events
  18.         proc.EnableRaisingEvents = True
  19.         AddHandler proc.Exited, AddressOf ProcExited
  20.  
  21.         proc.Start()
  22.         proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
  23.         proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
  24.     End Sub
  25.  
  26.     Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
  27.         MessageBox.Show(proc.StandardOutput.ReadToEnd)
  28.     End Sub
  29. End Class
  30.  
If you want to display the StandardOutput in a TextBox which is a control it will cause a cross thread error unless you use a Delegate like this
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim proc As Process
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         StartMyProcess()
  6.     End Sub
  7.  
  8.     Private Sub StartMyProcess()
  9.         proc = New Process
  10.         proc.StartInfo.FileName = "cmd.exe"
  11.         proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  12.         proc.StartInfo.CreateNoWindow = False
  13.         proc.StartInfo.UseShellExecute = False
  14.         proc.StartInfo.RedirectStandardOutput = True
  15.         proc.StartInfo.RedirectStandardInput = True
  16.  
  17.         'Added these two lines of code to enable the process to raise events
  18.         proc.EnableRaisingEvents = True
  19.         AddHandler proc.Exited, AddressOf ProcExited
  20.  
  21.         proc.Start()
  22.         proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
  23.         proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
  24.     End Sub
  25.  
  26.     Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
  27.         DisplayMessage(proc.StandardOutput.ReadToEnd)
  28.     End Sub
  29.  
  30.     Private Delegate Sub DisplayMessageDel(ByVal outputstring As String)
  31.  
  32.     Private Sub DisplayMessage(ByVal outputstring As String)
  33.         If TextBox1.InvokeRequired Then
  34.             Dim D As New DisplayMessageDel(AddressOf DisplayMessage)
  35.             TextBox1.Invoke(D, outputstring)
  36.             Exit Sub
  37.         End If
  38.         Dim output() As String = outputstring.Split(CChar(vbLf))
  39.         For x As Integer = 3 To output.Length - 1
  40.             If Not output(x).StartsWith("C:\") Then
  41.                 TextBox1.Text &= output(x).Trim(CChar(vbCr)) & vbNewLine
  42.             End If
  43.         Next
  44.     End Sub
  45. End Class
  46.  
Hope this helps. :)

Share this Question
Share on Google+
5 Replies


P: 7
No one can advice? I really need to know how to do this....
Apr 26 '13 #2

IronRazer
P: 83
Hi,
I don`t know if this will help you or not but, here is a way to make the process wait until it has exited and then write the StandardOutput to a textbox.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         StartMyProcess()
  5.     End Sub
  6.  
  7.     Sub StartMyProcess()
  8.         Dim proc As New Process
  9.         proc.StartInfo.FileName = "cmd.exe"
  10.         proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  11.         proc.StartInfo.CreateNoWindow = False
  12.         proc.StartInfo.UseShellExecute = False
  13.         proc.StartInfo.RedirectStandardOutput = True
  14.         proc.StartInfo.RedirectStandardInput = True
  15.         proc.Start()
  16.  
  17.         proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
  18.         proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
  19.  
  20.         proc.WaitForExit() 'This makes the process wait untill the cmd window has exited
  21.  
  22.         Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
  23.         For x As Integer = 3 To output.Length - 1
  24.             If Not output(x).StartsWith("C:\") Then
  25.                 TextBox1.Text &= output(x).Trim(CChar(vbCr)) & vbNewLine
  26.             End If
  27.         Next
  28.     End Sub
  29.  
  30. End Class
  31.  
Apr 30 '13 #3

P: 7
Thanks, but, I do not wanted to use the until exit method is causing the whole thing is hanging there - i still need to do something else in between.

@IronRazer
Apr 30 '13 #4

IronRazer
P: 83
Hi,
If you want to display the StandardOutput in a messagebox you can do it like this without freezing the program thread. However you may want to filter the output string before displaying it.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim proc As Process
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         StartMyProcess()
  6.     End Sub
  7.  
  8.     Private Sub StartMyProcess()
  9.         proc = New Process
  10.         proc.StartInfo.FileName = "cmd.exe"
  11.         proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  12.         proc.StartInfo.CreateNoWindow = False
  13.         proc.StartInfo.UseShellExecute = False
  14.         proc.StartInfo.RedirectStandardOutput = True
  15.         proc.StartInfo.RedirectStandardInput = True
  16.  
  17.         'Added these two lines of code to enable the process to raise events
  18.         proc.EnableRaisingEvents = True
  19.         AddHandler proc.Exited, AddressOf ProcExited
  20.  
  21.         proc.Start()
  22.         proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
  23.         proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
  24.     End Sub
  25.  
  26.     Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
  27.         MessageBox.Show(proc.StandardOutput.ReadToEnd)
  28.     End Sub
  29. End Class
  30.  
If you want to display the StandardOutput in a TextBox which is a control it will cause a cross thread error unless you use a Delegate like this
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim proc As Process
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         StartMyProcess()
  6.     End Sub
  7.  
  8.     Private Sub StartMyProcess()
  9.         proc = New Process
  10.         proc.StartInfo.FileName = "cmd.exe"
  11.         proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
  12.         proc.StartInfo.CreateNoWindow = False
  13.         proc.StartInfo.UseShellExecute = False
  14.         proc.StartInfo.RedirectStandardOutput = True
  15.         proc.StartInfo.RedirectStandardInput = True
  16.  
  17.         'Added these two lines of code to enable the process to raise events
  18.         proc.EnableRaisingEvents = True
  19.         AddHandler proc.Exited, AddressOf ProcExited
  20.  
  21.         proc.Start()
  22.         proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
  23.         proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
  24.     End Sub
  25.  
  26.     Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
  27.         DisplayMessage(proc.StandardOutput.ReadToEnd)
  28.     End Sub
  29.  
  30.     Private Delegate Sub DisplayMessageDel(ByVal outputstring As String)
  31.  
  32.     Private Sub DisplayMessage(ByVal outputstring As String)
  33.         If TextBox1.InvokeRequired Then
  34.             Dim D As New DisplayMessageDel(AddressOf DisplayMessage)
  35.             TextBox1.Invoke(D, outputstring)
  36.             Exit Sub
  37.         End If
  38.         Dim output() As String = outputstring.Split(CChar(vbLf))
  39.         For x As Integer = 3 To output.Length - 1
  40.             If Not output(x).StartsWith("C:\") Then
  41.                 TextBox1.Text &= output(x).Trim(CChar(vbCr)) & vbNewLine
  42.             End If
  43.         Next
  44.     End Sub
  45. End Class
  46.  
Hope this helps. :)
Apr 30 '13 #5

P: 7
Razor, Thank you so much. You gave me exactly what I need! Thanks! :)
May 11 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.