VB.NET通过PID(ProcessID)获得某进程的CPU使用率

来源:互联网 发布:锐捷mac客户端最新 编辑:程序博客网 时间:2024/06/06 17:07

查了一些资料,最简单的就是通过.NET提供的性能计数器获取,代码如下:

Imports System.DiagnosticsModule Module1    Sub Main()        Dim strCPU As String, strMemory As String, PID As UInteger = 0, MyProcess As Process        Console.Write("请输入进程标识符(PID):")        PID = Convert.ToUInt32(Console.ReadLine)        Try            MyProcess = Process.GetProcessById(PID)            Dim pcProcess As New PerformanceCounter("Process", "% Processor Time", MyProcess.ProcessName)            While True                strCPU = CInt(pcProcess.NextValue()).ToString + " %"                strMemory = (MyProcess.WorkingSet64 \ 1024).ToString + " K"                Console.WriteLine("PID: {0}, Name: {1}, CPU usage: {2}, Memory: {3}", _                                  PID.ToString, MyProcess.ProcessName, strCPU, strMemory)                System.Threading.Thread.Sleep(1000)            End While        Catch ex As Exception            Console.WriteLine("ERROR: " & ex.Message)        End Try    End SubEnd Module

这里写图片描述

0 0
原创粉丝点击