473,511 Members | 15,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know external application exit

7 New Member
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
5 2154
sheauyun
7 New Member
No one can advice? I really need to know how to do this....
Apr 26 '13 #2
IronRazer
83 New Member
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
sheauyun
7 New Member
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
83 New Member
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
sheauyun
7 New Member
Razor, Thank you so much. You gave me exactly what I need! Thanks! :)
May 11 '13 #6

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

Similar topics

1
8487
by: Guinness Mann | last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's the only .NET newsgroup I read and I'm certain someone here can help me. I have a C# program that checks for an error...
6
20038
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
4
10841
by: Bob Day | last post by:
Using VS 2003, VB.net... I am confused about the Application.Exit method, where the help states "This method does not force the application to exit." Aside from the naming confusion, how do I...
2
1781
by: Steven | last post by:
Hi, I have a program that monitors a few running applications. This monitoring program does not itself start these running applications. My question is how can the monitoring program detect that...
5
2461
by: Doug Handler | last post by:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new appname()) is run twice. I have in my constructor to do a check for a settings file, and if it isn't found, the...
2
2118
by: Lonifasiko | last post by:
Hi group, I must launch from my Winforms application another application (.exe developed by the client) which response time sometimes can be more or less 30 seconds. I'm using ProcessManager...
1
7019
by: Chris Cairns | last post by:
I have a MDI Application and would like to prompt the user before exit. I placed the following in the FormClosing event. It appears to work properly, however when a user answers no to the...
4
4860
by: Rico | last post by:
Hello, I have an Access application and I would like to not only run an external application, but at the point in the code where it is run, I'd like to wait for the external applicaiton to...
4
8470
by: =?Utf-8?B?TEJU?= | last post by:
Good day, I would like to execute an external application from a web form created using ASP.Net. I'm using System.Diagnostics.Process. It works fine if it is notepad.exe but it is not able to...
1
1598
by: JasonT | last post by:
Hi. I have created a dll in VC++ 08 that exports a CLR windows form class. I call into this dll from an external (closed source, third-party) application to instantiate the form and embed it into...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.