TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Process Examples (Process.Start)

Use the Process.Start Function. Open Microsoft Word, web browsers and text files.
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. 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.

VB.NET program that uses Start Module Module1 Sub Main() Process.Start("C:\") 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. Process.Start("example.txt") 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 SearchDuckDuckGo() is called with a String parameter. The code is then run, and it calls Process.Start.

Note: You can specify any web address. The default browser on Windows is chosen automatically.

VB.NET program that launches web browser Module Module1 Sub Main() SearchDuckDuckGo("cat pictures") End Sub Private Sub SearchDuckDuckGo(ByVal term As String) ' Search DuckDuckGo for the specified term. ' ... Default browser is used. Process.Start("https://www.duckduckgo.com/?q=" & term) 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() OpenMicrosoftWord("C:\Users\Sam\Documents\Office\Gears.docx") 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 Process.Start(startInfo) End Sub End Module
ProcessStartInfo. This class stores information about the process we want to run. We pass ProcessStartInfo to the Start Subroutine once we have assigned all its properties.

Arguments: This property stores the arguments, including any -flags or filenames. We concatenate strings together for the full string.

7-Zip Command-Line

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

CreateNoWindow: Specifies that we 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.

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 Process.Start(p) End Sub End Module
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.

For Each, For

Uses: With GetProcesses, we can scan the process list to see if any instances of a certain application (like Excel) are running.

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

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()) Next End Sub End Module Output Count: 65 chrome/5116 LogonUI/3736 atiesrxx/832 svchost/1760 svchost/3136 svchost/768 firefox/2540 ...
Kill. Let us examine the Kill Sub. You can also kill a Process you have started using VB.NET code, or one that you accessed that was already running.

Example: This simple program starts the "notepad" executable, which you are familiar with. It uses the Process.Start function for this.

Finally: It demonstrates the Kill method. At this point, the "notepad" process is no longer running.

VB.NET program that uses Kill Module Module1 Sub Main() ' Start the notepad.exe Process. Dim p As Process = Process.Start("notepad") Console.WriteLine("Started") ' Sleep for one second. Threading.Thread.Sleep(1000) ' Terminate. p.Kill() Console.WriteLine("Killed") End Sub End Module
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.

Threads, Sleep. Instead of a process, we can a use a Thread. The Sleep method pauses a program. It just ceases execution for a specified amount of time.Sleep
Threads, SyncLock. The SyncLock construct is useful when dealing with threads. It prevents multiple threads from accessing a piece of code at once.SyncLock
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf