Browse DevX
Sign up for e-mail newsletters from DevX


Launching and Monitoring External Programs from VB.NET Applications : Page 2

You no longer need to use the Win32 API or the VB Shell function to launch external applications. Instead, use the System.Diagnostics.Process class built into the .NET framework to simplify your code.

Monitoring Launched Processes
So far, the launched processes you've seen behave in an asynchronous manner; just like the classic VB Shell function. In other words, after launching the process, code in the parent program continues to execute. You need some way to monitor the launched process and find out when it exits—or sometimes, whether it's still running. Depending on your application, you may need to approach the problem in any of several different ways.

  • You want to launch the process, halting your program until it exits.
  • You want to launch the process, monitor it, and do something only when it ends, letting your program run normally in the meantime.
  • You want to launch the process, give it some input, let it process the input, and then force it to exit.
  • You want to launch the process and do something only as long as the launched process is running or is running without problems. If the process exits or stalls, you want to take some action.
  • You want to launch the process and give it some specific input, and/or retrieve the output for further processing. For example, you might want to launch a command window, programmatically type something into the window, and then retrieve and process the output.
Launch a Process and Wait Until It Exits
The simplest way to wait for a launched process to end is to call the Process.WaitForExit method. That causes the launching process to stop executing until the launched process exits. Unfortunately, when you use this directly from a Windows Form, it also causes the form to stop responding to system events, such as Paint.

So you wouldn't normally want to use the WaitForExit method to launch an external program from a Button (although it's perfectly appropriate to use the WaitForExit method to launch a second process from an application that has no visible user interface, such as calling a console application from the server in an ASP.NET application). The sample form has a button called "Launch and WaitForExit" (see Figure 2) that lets you see what happens when you use this method from a form.

Figure 2: The sample form lets you test and experiment with various ways of launching a process.
   Private Sub btnWaitForExit_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnWaitForExit.Click
      ' create a new process
      Dim myProcess As Process = _
      ' wait until it exits
      ' display results
      MessageBox.Show("Notepad was closed at: " & _
         myProcess.ExitTime & "." & _
         System.Environment.NewLine & "Exit Code: " & _
   End Sub
The preceding example illustrates an interesting point. Even though the launched process has closed, you still have the ability to access the Process object in code; however, at that point, most of the Process properties are unavailable, because the process itself no longer exists. You can still read the ExitCode and ExitTime properties, which return Integer and DateTime values. DOS commands set an exit code that lets you know whether errors occurred. .NET and other Windows applications can set the value by using the return value of the main method. By default, the value is zero. For DOS commands, a non-zero ExitCode value indicates either an error or that the command process was closed abnormally.

Author's Note: When you use the Process.Start method from a process instance, you should also call Process.Close after the process exits, to free the memory associated with the Process object.

Thanks for your registration, follow us on our social networks to keep up-to-date