Write a console utility to list processes

The Process class provides all you need to create a command-line utility that lists all the processes running on the system and all the related information. This utility is therefore similar to the Windows Task Manager, except you can run it from the command prompt. All processes are sorted alphabetically.

The following routine uses the routines FormatValue and FormatMemorySize, that are available elsewhere in our Code Bank. You should compile it as a console application with the name “Processes.”

Imports System.DiagnosticsModule Module1    Sub Main()        ' get all running processes        Dim procs() As Process = Process.GetProcesses        ' create a parallel array of process names        Dim procNames(procs.Length - 1) As String        Dim i As Integer        For i = 0 To procNames.Length - 1            procNames(i) = GetProcessName(procs(i))        Next        ' sort the two arrays        Array.Sort(procNames, procs)        Console.WriteLine("PROCESS NAME___________PID__STARTED_CPU " _            & "TIME___MEMORY_THREADS_HANDLES____")        Console.WriteLine()        For i = 0 To procs.Length - 1            Dim sb As New System.Text.StringBuilder()            Dim p As Process = procs(i)            Try                ' the process name is already in the procNames array                sb.Append(FormatValue(procNames(i), 20, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' the process ID                sb.Append(FormatValue(p.Id, 5, FormatColumnAlignment.Right))                sb.Append(" "c)                ' start time                sb.Append(FormatValue(p.StartTime.TimeOfDay, 8, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' the CPU time                sb.Append(FormatValue(p.TotalProcessorTime, 8, _                    FormatColumnAlignment.Left))                sb.Append(" "c)                ' memory usage                sb.Append(FormatValue(FormatMemorySize(p.WorkingSet, _                    FormatMemorySizeUnits.Kilobytes), 8, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                ' number of threads                sb.Append(FormatValue(p.Threads.Count, 7, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                ' number of handles                sb.Append(FormatValue(p.HandleCount, 7, _                    FormatColumnAlignment.Right))                sb.Append(" "c)                Console.WriteLine(sb.ToString)            Catch ex As Exception                ' ignore exceptions            End Try        Next    End Sub    ' get the name of a process    Function GetProcessName(ByVal p As Process) As String        Try            ' the process name            Dim procName As String = p.ProcessName            If Not p.MainModule Is Nothing Then                procName = p.MainModule.ModuleName            End If            Return procName        Catch ex As Exception            ' ignore exceptions        End Try    End FunctionEnd Module

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job