Vb.net Net获取程序的CPU使用率

Vb.net Net获取程序的CPU使用率,vb.net,cpu,Vb.net,Cpu,是否可以获取您在VB.Net中运行的自己的程序的CPU使用率? 我想添加一个CPU检测,如果程序的CPU高于例如10%,它会增加一个定时器数据来减少CPU。首先声明一个性能计数器 Private CPUPerf As New PerformanceCounter() 然后初始化它以获取CPU信息 With CPUPerf .CategoryName = "Processor" .CounterName = "% Proces

是否可以获取您在VB.Net中运行的自己的程序的CPU使用率?
我想添加一个CPU检测,如果程序的CPU高于例如10%,它会增加一个定时器数据来减少CPU。

首先声明一个性能计数器

Private CPUPerf As New PerformanceCounter()
然后初始化它以获取CPU信息

    With CPUPerf
        .CategoryName = "Processor"
        .CounterName = "% Processor Time"
        .InstanceName = "_Total"
    End With
然后您可以访问该值

    CPUPerf.NextValue

有关详细信息,请参阅。

首先声明PerformanceCounter

Private CPUPerf As New PerformanceCounter()
然后初始化它以获取CPU信息

    With CPUPerf
        .CategoryName = "Processor"
        .CounterName = "% Processor Time"
        .InstanceName = "_Total"
    End With
然后您可以访问该值

    CPUPerf.NextValue

有关更多信息,请参阅。

您可以使用类似的方法。
过程
性能计数器
计算机
所拥有的信息多种多样。 把这些信息放在一起,你可以通过你的进程获得与内存使用有关的信息。 另外,通过
Process
您可以获得有关应用程序的其他信息,如最大cpu使用率、时间使用率等

    Using currentP As Process = Process.GetCurrentProcess

        Dim bFactor As Double = 1000 / 1024
        Dim cMemory As Long = New PerformanceCounter("Process", "Working Set - Private", currentP.ProcessName).RawValue
        Dim sbInfo As StringBuilder = New StringBuilder

        sbInfo.AppendLine("Current ProcessName    : " & currentP.ProcessName)
        sbInfo.AppendLine("Current WorkingSet     : " & (cMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("In a total system PM   : " & (My.Computer.Info.TotalPhysicalMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("Percentage of all      : " & ((cMemory / My.Computer.Info.TotalPhysicalMemory) * 0.01).ToString("N6"))

        'MsgBox(sbInfo.ToString)
        Console.WriteLine(sbInfo.ToString)
    End Using

你可以用这样的东西。
过程
性能计数器
计算机
所拥有的信息多种多样。 把这些信息放在一起,你可以通过你的进程获得与内存使用有关的信息。 另外,通过
Process
您可以获得有关应用程序的其他信息,如最大cpu使用率、时间使用率等

    Using currentP As Process = Process.GetCurrentProcess

        Dim bFactor As Double = 1000 / 1024
        Dim cMemory As Long = New PerformanceCounter("Process", "Working Set - Private", currentP.ProcessName).RawValue
        Dim sbInfo As StringBuilder = New StringBuilder

        sbInfo.AppendLine("Current ProcessName    : " & currentP.ProcessName)
        sbInfo.AppendLine("Current WorkingSet     : " & (cMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("In a total system PM   : " & (My.Computer.Info.TotalPhysicalMemory * bFactor * 0.000001).ToString & " MB ")
        sbInfo.AppendLine("Percentage of all      : " & ((cMemory / My.Computer.Info.TotalPhysicalMemory) * 0.01).ToString("N6"))

        'MsgBox(sbInfo.ToString)
        Console.WriteLine(sbInfo.ToString)
    End Using

看看对于C#和任意进程,将其答案转换为VB.NET和您自己的进程是很简单的。非常感谢您的帮助:)请看。对于C#和任意进程,将其答案转换为VB.NET和您自己的进程是很简单的。非常感谢您的帮助:)所以我将使用“MessageBox.Show(CPUPerf.NextValue)”来显示程序的CPU使用情况?它对我说0,但程序的CPU使用率为4.2%。当然。测量使用率是不精确的,将基于采样的时间。因此,我将使用“MessageBox.Show(CPUPerf.NextValue)”来显示程序的CPU使用率?它对我说0,但程序的CPU使用率为4.2%。当然。测量用途不精确,将以取样时间为基础。