VB.NET工作笔记015---vb.net获取cpu使用率,内存使用率_未能找到具有指定类别名“Processor”、计数器名“% Processor Time”的性能计数

vb.net获取系统CPU的使用率,和内存的使用率

获取cpu的使用率,用:

VB.NET工作笔记015---vb.net获取cpu使用率,内存使用率_未能找到具有指定类别名“Processor”、计数器名“% Processor Time”的性能计数

performanceCounter1这个控件,一定要记得,写上Processor

这个:

counterName

categoryName

instanceName

这几个属性一定要写上,如果不写上就会报错.

未能找到具有指定类别名“Processor”、计数器名“% Processor Time”的性能计数

 

Dim systemCpuCount As Double = 0
        systemCpuCount = Convert.ToDouble(PerformanceCounter1.NextValue)

然后获取的时候,直接.NextValue就可以获取cpu的使用率了.

当然也可以获取cpu的,空闲使用率,然后100减去这个数,就可以得到cpu使用率了,但是

注意cpu的计算,跟任务管理器的cpu使用率显示的不一样.应该是只是计算的一个核心的

cpu使用率吧.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

另外关于内存的计算,也有代码实现:

可以从下面的代码中找出这部分来用

Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Threading
Imports System.Collections


Public Class interComFrm
    Private Declare Function GlobalMemoryStatusEx Lib "kernel32" Alias "GlobalMemoryStatusEx" (
     <[In](), Out()> ByVal lpBuffer As MEMORYSTATUSEX
     ) As <MarshalAs(UnmanagedType.Bool)> Boolean

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Public Class MEMORYSTATUSEX

        ''' <summary>
        ''' Initializes a new instance of the <see cref="T:MEMORYSTATUSEX" /> class.
        ''' </summary>
        Public Sub New()
            Me.dwLength = CType(Marshal.SizeOf(GetType(MEMORYSTATUSEX)), UInt32)
        End Sub
        ' Fields
        ''' <summary>
        ''' Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.
        ''' </summary>
        Public dwLength As UInt32
        ''' <summary>
        ''' Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
        ''' </summary>
        Public dwMemoryLoad As UInt32
        ''' <summary>
        ''' Total size of physical memory, in bytes.
        ''' </summary>
        Public ullTotalPhys As UInt64
        ''' <summary>
        ''' Size of physical memory available, in bytes.
        ''' </summary>
        Public ullAvailPhys As UInt64
        ''' <summary>
        ''' Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
        ''' </summary>
        Public ullTotalPageFile As UInt64
        ''' <summary>
        ''' Size of available memory to commit, in bytes. The limit is ullTotalPageFile.
        ''' </summary>
        Public ullAvailPageFile As UInt64
        ''' <summary>
        ''' Total size of the user mode portion of the virtual address space of the calling process, in bytes.
        ''' </summary>
        Public ullTotalVirtual As UInt64
        ''' <summary>
        ''' Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
        ''' </summary>
        Public ullAvailVirtual As UInt64
        ''' <summary>
        ''' Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.
        ''' </summary>
        Public ullAvailExtendedVirtual As UInt64
    End Class

    Private isNormal As Boolean = False

    '4.检查cpu和内存有没有超过100
    ' Private CPUCounter As New PerformanceCounter("Processor", "% Processor Time", "_Total")
    'Private CPUCounter As PerformanceCounter
    '5.内存使用情况
    Private memInfo As New MEMORYSTATUSEX
    Private memBln As Boolean = False

    Private startCount As Integer = 0
    Private txtStartNum As Integer = 0
    Private systemCpuCount As Double = 10
    Private cpuPerCount As Integer = 0
    Private cpuAllCount As Double = 0
    Private interComCountNum As Integer = 0

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        '3.获取起始号码
        'Dim CPUCounter As New PerformanceCounter("Processor", "% Processor Time", "_Total")
        'Dim a As Double = CPUCounter.NextValue
        txtStartNum = Convert.ToInt32(Trim(txtStart.Text))
        PerformanceCounter1.NextValue()
        systemCpuCount = 10
        'Dim CPUCounter As New PerformanceCounter("Processor", "% Processor Time", "_Total")
        ' CPUCounter = New PerformanceCounter("Processor", "% Processor Time", "_Total")
        TimerCpu.Enabled = True
        Sleep(100)
        timStatus.Enabled = True
        Sleep(1000)
        timAction.Enabled = True
        'txtStart.Text = 1
        lblCount.Text = 0
        '4.检查cpu和内存有没有超过100
        'Dim CPUCounter As New PerformanceCounter("Processor", "% Processor Time", "_Total")
        'Dim systemCpuCount As Double = Convert.ToDouble(CPUCounter.NextValue)
        '5.内存使用情况
        'Dim memInfo As New MEMORYSTATUSEX
        'Dim memBln As Boolean = False


    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles timStatus.Tick
        memBln = GlobalMemoryStatusEx(memInfo)
        Dim systemMemCount As Double = Convert.ToDouble(memInfo.dwMemoryLoad.ToString)
        ' Dim systemCpuCount As Double = 0
        ' systemCpuCount = Convert.ToDouble(PerformanceCounter1.NextValue)
        '6.显示CPU和内存的使用情况
        lblCPU.Text = Convert.ToString(Math.Round(systemCpuCount, 2)) + "%"

        lblMem.Text = Convert.ToString(systemMemCount) + "%"
        '7.添加状态
        lblStatus.BackColor = Color.White

        If isNormal Then
            lblStatus.BackColor = Color.SpringGreen
        Else
            lblStatus.BackColor = Color.Red
        End If

    End Sub
    Public Shared Sub Sleep(ByVal Interval)
        Dim __time As DateTime = DateTime.Now
        Dim __Span As Int64 = Interval * 10000 '因为时间是以100纳秒为单位。 
        While (DateTime.Now.Ticks - __time.Ticks < __Span)
            Application.DoEvents()
        End While
    End Sub

    Private Sub timAction_Tick(sender As Object, e As EventArgs) Handles timAction.Tick
        memBln = GlobalMemoryStatusEx(memInfo)
        Dim systemMemCount As Double = Convert.ToDouble(memInfo.dwMemoryLoad.ToString)

        '启动当前目录下
        Dim proc As New Process
        proc.StartInfo.FileName = "NetIntercomClient.exe"
        proc.Start()
        Sleep(800)
        SendKeys.SendWait("{END}")
        Sleep(200)
        SendKeys.SendWait(txtStartNum)
        txtStartNum = txtStartNum + 1
        interComCountNum = interComCountNum + 1
        lblCount.Text = interComCountNum
        Sleep(200)
        SendKeys.SendWait("%{C}")

        If systemCpuCount >= 98 OrElse systemMemCount >= 98 Then
            isNormal = False
            timAction.Enabled = False
        End If


        'CPUCounter

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '1.获取起始号码
        'Dim txtStartNum As Integer = 0
        '2.清空状态信息
        txtStart.Text = 1
        isNormal = True
        lblCPU.Text = "10%"
        lblMem.Text = "10%"
        lblCount.Text = 0

        TimerCpu.Enabled = True
        Sleep(100)
        timStatus.Enabled = True
        Sleep(1000)

    End Sub

    Private Sub TimerCpu_Tick(sender As Object, e As EventArgs) Handles TimerCpu.Tick
        Dim cpuPercent As Double = 0

        cpuPerCount = cpuPerCount + 1
        cpuAllCount = cpuAllCount + PerformanceCounter1.NextValue

        If cpuPerCount = 30 Then
            systemCpuCount = cpuAllCount / cpuPerCount
            systemCpuCount = (100 - systemCpuCount)
            If systemCpuCount <= 30 Then
                systemCpuCount = systemCpuCount + 6
            ElseIf systemCpuCount <= 40 Then
                systemCpuCount = systemCpuCount + 12
            ElseIf systemCpuCount <= 48 Then
                systemCpuCount = systemCpuCount + 50
            ElseIf systemCpuCount <= 50 Then
                systemCpuCount = systemCpuCount + 46
            ElseIf systemCpuCount <= 55 Then
                systemCpuCount = systemCpuCount + 43
            ElseIf systemCpuCount <= 58 Then
                systemCpuCount = systemCpuCount + 41
            ElseIf systemCpuCount <= 60 Then
                systemCpuCount = systemCpuCount + 38
            ElseIf systemCpuCount <= 65 Then
                systemCpuCount = systemCpuCount + 39
            ElseIf systemCpuCount <= 70 Then
                systemCpuCount = systemCpuCount + 29
            End If
            cpuPerCount = 0
            cpuAllCount = 0
        End If

    End Sub

    Private Sub interComFrm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

    End Sub
End Class

 

 

 

 

上一篇:【LeetCode-树】从前序与中序遍历序列构造二叉树


下一篇:VB.NET在WinForm中嵌入谷歌浏览器_制作全屏显示网页程序_并读取INI配置文件_根据配置文件显示不同的网页---VB.NET工作笔记016