With the VB.NET Process type, from System.Diagnostics
, we launch processes directly inside programs. We can run an external EXE this way.
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
.
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 ModuleSaving 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.
Length
property.For-Each
loop to enumerate the Process objects. We display the ProcessName
and the Id of each Process.GetProcesses
, we can scan the process list to see if any instances of a certain application (like Excel) are running.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 ModuleCount: 65 chrome/5116 LogonUI/3736 atiesrxx/832 svchost/1760 svchost/3136 svchost/768 firefox/2540 ...
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.
Process.Start
function for this.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
Processes are operating system level threads. They are started with the Process.Start
function. By starting processes, we introduce many new capabilities to programs.