VB.Net获取程序的CPU使用率 [英] VB.Net get CPU Usage of Program

查看:58
本文介绍了VB.Net获取程序的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获得在VB.Net中运行的自己程序的CPU使用率?我想添加一个CPU检测,以防本程序的CPU高于例如10%,这会增加计时器延迟以减少CPU.

Is it possible to get the CPU Usage of the own Program that you are running in VB.Net? I would like to add a CPU detection, in case the CPU of the Program is above for example 10% it increases a timer deleay to reduce the CPU.

推荐答案

您可以使用类似这样的东西. Process PerformanceCounter Computer 具有多种信息.将这些信息放在一起,就可以通过进程获得与SO内存使用有关的信息.此外,通过 Process ,您还可以获得有关您的应用程序的其他信息,例如最大CPU使用率,时间使用率等.

You can use something like this. There is a mix of informations had by Process, PerformanceCounter and Computer. Putting together those informations you can get information by your process in relation con SO memory usage. Also by Process you can get other informations about your app like max cpu usage, time usage etc.

    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

这篇关于VB.Net获取程序的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆