VB.NET Process Examples (Process.Start)Use the Process.Start Function. Open Microsoft Word, web browsers and text files.
dot net perls
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.
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.
VB.NET program that uses Process.Start
' 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 "
' Start the process.
Saving file 'c:\programs\test.webp'
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 |
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
' Get processes.
Dim processes() As Process = Process.GetProcesses
' Loop over processes.
For Each p As Process In processes
' Display process properties.
Console.WriteLine(p.ProcessName + "/"
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
' Start the notepad.exe Process.
Dim p As Process = Process.Start("notepad"
' Sleep for one second.
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.
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.
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.
© 2007-2021 sam allen. send bug reports to firstname.lastname@example.org.