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

获取cpu的使用率,用:

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.CollectionsPublic Class interComFrmPrivate 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 UInt64End ClassPrivate 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 MEMORYSTATUSEXPrivate memBln As Boolean = FalsePrivate startCount As Integer = 0Private txtStartNum As Integer = 0Private systemCpuCount As Double = 10Private cpuPerCount As Integer = 0Private cpuAllCount As Double = 0Private interComCountNum As Integer = 0Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs)End SubPrivate 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.NextValuetxtStartNum = 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 = TrueSleep(100)timStatus.Enabled = TrueSleep(1000)timAction.Enabled = True'txtStart.Text = 1lblCount.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 = FalseEnd SubPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles timStatus.TickmemBln = 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.WhiteIf isNormal ThenlblStatus.BackColor = Color.SpringGreenElselblStatus.BackColor = Color.RedEnd IfEnd SubPublic Shared Sub Sleep(ByVal Interval)Dim __time As DateTime = DateTime.NowDim __Span As Int64 = Interval * 10000 '因为时间是以100纳秒为单位。 While (DateTime.Now.Ticks - __time.Ticks < __Span)Application.DoEvents()End WhileEnd SubPrivate Sub timAction_Tick(sender As Object, e As EventArgs) Handles timAction.TickmemBln = GlobalMemoryStatusEx(memInfo)Dim systemMemCount As Double = Convert.ToDouble(memInfo.dwMemoryLoad.ToString)'启动当前目录下Dim proc As New Processproc.StartInfo.FileName = "NetIntercomClient.exe"proc.Start()Sleep(800)SendKeys.SendWait("{END}")Sleep(200)SendKeys.SendWait(txtStartNum)txtStartNum = txtStartNum + 1interComCountNum = interComCountNum + 1lblCount.Text = interComCountNumSleep(200)SendKeys.SendWait("%{C}")If systemCpuCount >= 98 OrElse systemMemCount >= 98 ThenisNormal = FalsetimAction.Enabled = FalseEnd If'CPUCounterEnd SubPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load'1.获取起始号码'Dim txtStartNum As Integer = 0'2.清空状态信息txtStart.Text = 1isNormal = TruelblCPU.Text = "10%"lblMem.Text = "10%"lblCount.Text = 0TimerCpu.Enabled = TrueSleep(100)timStatus.Enabled = TrueSleep(1000)End SubPrivate Sub TimerCpu_Tick(sender As Object, e As EventArgs) Handles TimerCpu.TickDim cpuPercent As Double = 0cpuPerCount = cpuPerCount + 1cpuAllCount = cpuAllCount + PerformanceCounter1.NextValueIf cpuPerCount = 30 ThensystemCpuCount = cpuAllCount / cpuPerCountsystemCpuCount = (100 - systemCpuCount)If systemCpuCount <= 30 ThensystemCpuCount = systemCpuCount + 6ElseIf systemCpuCount <= 40 ThensystemCpuCount = systemCpuCount + 12ElseIf systemCpuCount <= 48 ThensystemCpuCount = systemCpuCount + 50ElseIf systemCpuCount <= 50 ThensystemCpuCount = systemCpuCount + 46ElseIf systemCpuCount <= 55 ThensystemCpuCount = systemCpuCount + 43ElseIf systemCpuCount <= 58 ThensystemCpuCount = systemCpuCount + 41ElseIf systemCpuCount <= 60 ThensystemCpuCount = systemCpuCount + 38ElseIf systemCpuCount <= 65 ThensystemCpuCount = systemCpuCount + 39ElseIf systemCpuCount <= 70 ThensystemCpuCount = systemCpuCount + 29End IfcpuPerCount = 0cpuAllCount = 0End IfEnd SubPrivate Sub interComFrm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosedEnd Sub
End Class

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

  1. python使用率_Python获取CPU、内存使用率以及网络使用状态代码

    由于psutil已更新到3.0.1版本,最新的代码如下: #!/usr/bin/env python import os import time import sys import atexit im ...

  2. java 收集系统资源_方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

    CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...

  3. C#获取特定进程CPU和内存使用率

    C#获取特定进程CPU和内存使用率 首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetCurrentProces ...

  4. Android获取cpu和内存信息、网址的代码

    android获取手机cpu并判断是单核还是多核 /**  * Gets the number of cores available in this device, across all proces ...

  5. Windows获取CPU、内存和磁盘使用率脚本

    获取CPU使用率脚本(vbs),另存为cpu.vbs: On Error Resume Next Set objProc = GetObject("winmgmts:\\.\root\cim ...

  6. 戴尔服务器r740硬盘指示灯,戴尔R740服务器获取cpu、内存、硬盘参数信息。

    戴尔R740服务器获取cpu.内存.硬盘参数信息.使用redfish协议,只使用了system的一个总URL即可获取所有参数. import requests import json requests ...

  7. 基于SNMP通过OID获取思科、锐捷、迈普等交换机的CPU、内存使用率

    一.基于SNMP通过OID获取思科交换机的CPU.内存使用率 1.获取CPU使用率的OID: 1.3.6.1.4.1.9.2.1.56.0 过去5秒中CPU使用率 1.3.6.1.4.1.9.2.1. ...

  8. 华为服务器怎么查看cpu型号,华为2288HV5获取cpu、内存、存储等参数信息

    华为2288HV5获取cpu.内存.存储等参数信息,采用redfish协议.华为的技术支撑团队很给力,获取资料很全面,讲解也很到位.所以第一个redfish案例就是用华为. import reques ...

  9. VB.NET工作笔记002---asp调用vb6编写的activex dll

    JAVA技术交流QQ群:170933152 注意这里下面写的,这个小项目,个人发现,仅仅写一个sayHello()方法就可以了,仅仅做个例子的话,不用这么麻烦还写这么多.. 打开VB6,新建Activ ...

最新文章

  1. android string.xml前后加空格的技巧
  2. os_mem.c(全)
  3. 面向.NET开发人员的Dapr——总结和前景
  4. [置顶] 自己动手写Web容器之TomJetty之六:动态页面引入
  5. LightGBM大战XGBoost,谁将夺得桂冠?
  6. ARM926EJ-S/ARM920T 协处理器 CP14, CP15详解(转载)
  7. cups ipp oracle,架设Linux打印服务器通过Web界面CUPS管理其他电脑访问CUPS web管理界面IPP协议共享本机打印机...
  8. 云计算、大数据、人工智能三者究竟有什么关系
  9. 云计算数据中心运维管理要点
  10. 黑客窃取企业数据的后门程序技巧
  11. html画布刮刮乐,h5canvas实现刮刮乐效果的方法
  12. 第三篇--编译CM系统
  13. 汽车电子技术——软考中级之系统集成项目管理工程师 高分(63+69)通过备考经验分享
  14. 前端埋点方案设计思路
  15. 幕布,为知笔记和Effie哪个更适合采编呢?
  16. 关于小程序开发的一些资源
  17. maven本地注册oracle ojdbc驱动(ojdbc7-12.2.0.1)
  18. 【IHE专题】IHE相关资源汇总
  19. Sunday and Monday
  20. c++ string http://www.byvoid.com/blog/cpp-string/

热门文章

  1. mysql查询所有姓王的信息_MySQL的查询练习
  2. FPGA学习笔记---任务(task) 和函数 (function)
  3. linux下darknet指定opencv4编译安装
  4. FBI局长重提解锁iPhone 希望展开关于智能手机加密的讨论
  5. 最短路径Shortest Path algorithm
  6. Android将应用调试log信息保存在SD卡
  7. 【个性化阅读】ZapTxt的提醒姗姗来迟
  8. scp命令报错-bash: scp: command not found
  9. NUnit单元测试笔记
  10. 表单的管理作业及答案