1. Technology

How to use Process.Start in Visual Basic .NET

When you need to start another application using VB.NET code

By

Man using laptop computer
PhotoAlto/Sigrid Olsson/PhotoAlto Agency RF Collections/Getty Images
Updated June 09, 2014

The Start method of the Process object is possibly one of the most underappreciated tools available to you as a programmer.

As a .NET method, Start has a series of overloads (different sets of parameters that determine exactly what the method does). In this case, the overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.

What you can do with Process.Start is really only limited by the processes you can use with it. Which is to say, there isn't any limit since more keep coming all the time. If you want to display your text-based "ReadMe" file in Notepad, it's as easy as:

Process.Start("ReadMe.txt")

or

Process.Start("notepad", "ReadMe.txt")

This assumes the ReadMe file is in the same folder as the program and that Notepad is the default application for .txt file types and it's in the system environment path.

For the VB6'ers in the audience, Process.Start is somewhat like the VB6 Shell command. In VB6, you could use something like:

lngPID = Shell("MyTextFile.txt", vbNormalFocus)

You can still call the Shell command in VB.NET, but I wouldn't recommend it. Use Process.Start instead.

You can use this code instead to start Notepad maximized and create a ProcessStartInfo object that you can use for more precise control:

Dim ProcessProperties As New ProcessStartInfo
ProcessProperties.FileName = "notepad"
ProcessProperties.Arguments = "myTextFile.txt"
ProcessProperties.WindowStyle = ProcessWindowStyle.Maximized
Dim myProcess As Process = Process.Start(ProcessProperties)

You can even start a hidden process ...

ProcessProperties.WindowStyle = ProcessWindowStyle.Hidden

But be careful ... unless you add more code to end the process, you'll probably have to end it in Task Manager. Hidden processes are normally only used with processes that don't have any kind of a user interface.

Working with Process.Start as a .NET object gives you a lot of capability. For example, you can retrieve the name of the process that was started. This code will display "notepad" in the Output window:

Dim myProcess As Process = Process.Start("MyTextFile.txt")
Console.WriteLine(myProcess.ProcessName)

If you need to suspend execution of your program until the process has ended (something you could do with API code in VB6), here ya go ...

myProcess.WaitForExit()

Console.WriteLine("Notepad ended: " _
   & myProcess.ExitTime & _
   Environment.NewLine & _
   "Exit Code: " & _
   myProcess.ExitCode)

This was something you could not do with the VB6 Shell command because it launched the new application asynchronously. Using WaitForExit can cause the reverse problem in .NET because you have to launch a process in a new thread if you need it to execute asynchronously. For example, if you need the components to remain active in a form where a process was launched and WaitForExit was executed. Ordinarily, those components won't be active. Code it up and see for yourself.

One way to force the process to halt is to use the Kill method.

myProcess.Kill()

This code waits for ten seconds and then ends the process. I found that a forced delay was necessary to allow the process to complete exiting to avoid an error.

myProcess.WaitForExit(10000)
' if the process doesn't complete within
' 10 seconds, kill it
If Not myProcess.HasExited Then
   myProcess.Kill()
End If
Threading.Thread.Sleep(1)
Console.WriteLine("Notepad ended: " _
   & myProcess.ExitTime & _
   Environment.NewLine & _
   "Exit Code: " & _
   myProcess.ExitCode)

In most cases, it's probably a good idea to put your processing in a Using block to ensure that the resources used by the process are released.

Using myProcess As Process = New Process
' Your code goes here
End Using

To make all this even easier to work with, there is even a Process component that you can add to your project so you can do a lot of the things shown above at design time instead of run time.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

One of the things that this makes a lot easier is coding events raised by the process, such as the event when the process has exited. You can also add a handler using code like this ...

' allow the process to raise events
myProcess.EnableRaisingEvents = True

' add an Exited event handler
AddHandler myProcess.Exited, _
   AddressOf Me.ProcessExited

Private Sub ProcessExited(ByVal sender As Object, _
   ByVal e As System.EventArgs)
' Your code goes here
End Sub

But simply selecting the event for the component is a lot easier.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Start Something - Using Process.Start in VB.NET

©2014 About.com. All rights reserved.