How to Use Process.Start in Visual Basic

When You Need to Start Another Application Using VB Code

Man using laptop computer
PhotoAlto/Sigrid Olsson/PhotoAlto Agency RF Collections/Getty Images

The Start method of the Process object is possibly one of the most underappreciated tools available to programmer. As a .NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. 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.

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.

Process.Start Similar to Shell Command in VB6

For programmers familiar with Visual Basic 6, Process.Start is somewhat like the VB 6 Shell command. In VB 6, you would use something like:

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

Using Process.Start

You can use this code 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)

Starting a Hidden Process

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.

Retrieving the Name of a Process

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

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.

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.