.NET Array Dictionary List String Sub ArrayList Cast Class Console Dates DataTable DateTime Enum File For Format If IndexOf Lambda LINQ Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder Substring

Process. A process starts, performs a task and ends. With the Process type, from System.Diagnostics, we launch processes directly inside programs.
Functions. Many new tasks become possible, with little extra complexity. With Start we create a Process. With GetProcesses we check existing ones.
First example. First, here is an example program. It uses Process.Start to open the file manager on your C:\ drive. When you run this example the root directory folder will open.

Tip:Process.Start, found in System.Diagnostics, launches external programs, such as Word, Excel, a web browser—EXE programs.

Main:The Main Sub calls the Process.Start Shared method. It passes one parameter to Process.Start, the directory root.

Based on: .NET 4.5

VB.NET program that uses Start

Module Module1
    Sub Main()
    End Sub
End Module
Text file. When you specify a certain file for Process.Start to open, the default Windows file viewer for the file type will open. This is useful for many files.

Tip:Instead of just passing "example.txt", you could pass "C:\Users\Sam\example.txt" to specify the absolute directory.

VB.NET program that opens text file

Module Module1
    Sub Main()
	' Open the file at the current program's directory.
	' It will appear in the default text file viewer.
    End Sub
End Module
Search. Resources can be given to the user in the form of URLs. You can tell Windows to launch a web browser window with a specific URL. We send Process.Start the URL.

Here:The subroutine SearchGoogle() is called with a String parameter. The SearchGoogle function is then run, and it calls Process.Start.

VB.NET program that launches web browser

Module Module1
    Sub Main()
	SearchGoogle("VB.NET awesome")
    End Sub

    ''' <summary>
    ''' Open the user's default browser and search for the parameter.
    ''' </summary>
    Private Sub SearchGoogle(ByVal t As String)
	Process.Start("http://google.com/search?q=" & t)
    End Sub
End Module
Microsoft Word. Many applications need to launch Word documents for editing or viewing. It is best to start Word in an external process. We use a custom FileName field and Arguments.

OpenMicrosoftWord:This code assigns WINWORD.EXE as the FileName. Then it sets the path of the DOCX file as the Arguments property.

Result:Microsoft Word opens the file. You will need to adjust the paths for it to correctly work.

VB.NET program that opens Microsoft Word

Module Module1
    Sub Main()
    End Sub

    ''' <summary>
    ''' Open the path parameter with Microsoft Word.
    ''' </summary>
    Private Sub OpenMicrosoftWord(ByVal f As String)
	Dim startInfo As New ProcessStartInfo
	startInfo.FileName = "WINWORD.EXE"
	startInfo.Arguments = f
    End Sub
End Module
Executable. Next, we use a more complex Arguments String and also assign the WindowStyle for a command-line program. The command line program shown here is located at C:\7za.exe.7-Zip Command-Line

Here:Two variable Strings are declared. The two strings indicate two argument parameters we want to use with the executable.

Arguments:The arguments String here is composed of five Strings concatenated together with the & operator.

VB.NET program that launches executable

Module Module1
    Sub Main()
	' One file parameter to the executable
	Dim sourceName As String = "ExampleText.txt"
	' The second file parameter to the executable
	Dim targetName As String = "Example.gz"

	' New ProcessStartInfo created
	Dim p As New ProcessStartInfo

	' Specify the location of the binary
	p.FileName = "C:\7za.exe"

	' Use these arguments for the process
	p.Arguments = "a -tgzip """ & targetName & """ """ &
	    sourceName & """ -mx=9"

	' Use a hidden window
	p.WindowStyle = ProcessWindowStyle.Hidden

	' Start the process
    End Sub
End Module
ProcessStartInfo. This class stores information about the process you want to run. It has fields that tell .NET how to start the FileName program. We specify property values.

FileName:This is the program or filename you want to run. You can set it to a file such as "example.txt" or an executable name.

Arguments:This property stores the arguments, including any -flags or filenames. It is a String value.

CreateNoWindow:Specifies that you want to run a command line program silently without flashing a console window.

WindowStyle:Use this to set windows as hidden, with the ProcessWindowStyle.Hidden enumeration value.

Shell. Internally this subroutine simply calls Process.Start. It is useful for compatibility reasons. If you have used it often, it may be more convenient.Shell

However:The Process type is probably better overall. More developers, such as those working in C#, may be accustomed to it.

Kill. You can also kill a Process you have started using VB.NET code, or one that you accessed that was already running. We cover some issues related to Kill.Kill

Caution:If you think a method called Kill might be dangerous, you are correct. This method can cause data loss in external processes.

GetProcesses. An operating system manages many processes. We can retrieve an Array of these processes with the Process.GetProcesses Function.

Example:We display the number of processes in the array by using the Length property.

Then:We use the For-Each loop to enumerate the Process objects. We display the ProcessName and the Id of each Process.

VB.NET program that uses Process.GetProcesses

Module Module1
    Sub Main()
	' Get processes.
	Dim processes() As Process = Process.GetProcesses()
	Console.WriteLine("Count: {0}", processes.Length)

	' Loop over processes.
	For Each p As Process In processes

	    ' Display process properties.
	    Console.WriteLine(p.ProcessName + "/" + p.Id.ToString())
    End Sub
End Module


Count: 65
GetProcesses, usage. The GetProcesses() Function has a variety of uses. In some programs, we start external programs such as Microsoft Excel.

Tip:With GetProcesses, we can scan the process list to see if any instances of Excel are running.

Tip 2:Process.GetProcesses() is effective in programs that analyze memory usage on a computer, in a diagnostic tool.

Threads. Long-running tasks will block a main thread. Instead of launching another process, we can a use a Thread. But threads add complexity.

Sleep:The Sleep method pauses a program. It does not peg the CPU. Rather it just ceases execution for a specified amount of time.


SyncLock:The SyncLock construct is useful when dealing with threads. It prevents multiple threads from accessing a piece of code at once.

ThreadPool. The ThreadPool type allows the efficient creation (and reuse) of threads. This approach is sometimes useful. It can enhance batch processing.ThreadPool
A summary. Processes are operating system level threads. They are started with the Process.Start function. By starting processes, we introduce many new capabilities to programs.