Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Write a console utility to list processes

Total Hit ( 4154)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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."

Click here to copy the following block
Imports System.Diagnostics

Module 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 Function
End Module


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.