Module Module1
Sub Main()
Dim cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total")
Dim pros As Process()
While True
pros = Process.GetProcesses()
Dim appname As String = "ffmpeg"
For Each pro As Process In pros
If pro.ProcessName = appname Then
Dim percentage = Math.Round(cpu.NextValue(), 2, MidpointRounding.AwayFromZero)
Console.WriteLine("CPU:" & percentage.ToString)
Exit For
End If
Next
Threading.Thread.Sleep(1000)
End While
End Sub
End Module
本文介绍了一段VB.NET代码,用于实时监测指定进程(如ffmpeg)的CPU使用率。通过创建PerformanceCounter实例并利用Process类获取系统中所有运行的进程,代码能够找到目标进程并读取其CPU使用百分比。
1332

被折叠的 条评论
为什么被折叠?



