Process Examples (Process.Start)Use the Process.Start Function. Open Microsoft Word, web browsers and text files.
This page was last reviewed on Oct 23, 2022.
Process. With the VB.NET Process type, from System.Diagnostics, we launch processes directly inside programs. We can run an external EXE this way.
Functions. Many new tasks become possible, with little extra complexity. With Start we create a Process. With GetProcesses we check existing ones.
Exe example. To use Process.Start with .NET Core, try adding an EXE in a known location on the disk. Specify the file name of this Exe as the FileName property of a ProcessStartInfo.
Tip Read the documentation (help) of the EXE, and figure out the correct arguments to pass to it.
Here We invoke a program called cwebp.exe, at the C directory root, to convert an image to a WebP file.
Result When we call Start(), the EXE runs and its code is executed. We can check the directory to see the converted image.
Module Module1 Sub Main() ' Argument file names. Dim sourceName As String = "c:\programs\test.png" Dim targetName As String = "c:\programs\test.webp" ' Create ProcessStartInfo. Dim p As New ProcessStartInfo p.FileName = "C:\cwebp.exe" ' Use these arguments for the process. p.Arguments = sourceName & " -o " & targetName ' Start the process. Process.Start(p) End Sub End Module
Saving file 'c:\programs\test.webp' File: c:\programs\test.png Dimension: 300 x 175 Output: 5664 bytes Y-U-V-All-PSNR 41.65 43.21 41.23 41.80 dB block count: intra4: 139 intra16: 70 (-> 33.49%) skipped block: 40 (19.14%) bytes used: header: 212 (3.7%) mode-partition: 588 (10.4%) Residuals bytes |segment 1|segment 2|segment 3|segment 4| total macroblocks: | 1%| 9%| 51%| 36%| 209 quantizer: | 36 | 33 | 27 | 20 | filter level: | 11 | 7 | 6 | 4 |
GetProcesses. An operating system manages many processes. We can retrieve an Array of these processes with the Process.GetProcesses Function.
Detail 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
Detail 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.
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
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.
Detail 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.
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.
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.
Threads, SyncLock. The SyncLock construct is useful when dealing with threads. It prevents multiple threads from accessing a piece of code at once.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.