Don't get pid approach, because the program I want to change name behind,
CodePudding user response:
Don't understand why so resist with PID, if the current Process, the Process. The GetCurrentProcess () method returns the current Process object, through this object may take PID is completely take wrong Process, is to take by ProcessName, likely the same Process would be unable to distinguish between problem (also not won't solve the problem),Method 1: using performance counters
Public Class Form1
Dim proc As Process
Dim cpuCounter As PerformanceCounter
Dim ramCounter As PerformanceCounter
Private Sub Timer1_Tick (sender As Object, As EventArgs e) Handles Timer1. Tick
Label1. Text=the String. Format (
"The CPU: {0} % MEM: {1} MB",
CpuCounter. NextValue (). The ToString (" # 0. # # "),
(ramCounter NextValue () & gt;> 16). ToString (" # # #, # # # # # "))
End Sub
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
Proc=Process. GetCurrentProcess ()
CpuCounter=New PerformanceCounter (" Process ", "% Processor Time", proc., ProcessName)
RamCounter=New PerformanceCounter (" Process ", "Working Set", proc., ProcessName)
End Sub
The End of the Class
Method 2:
Use Process object TotalProcessorTime to obtain the total CPU time (need to minus the last TotalProcessorTime and divided by the time interval, divided by the logical number)
100 * (proc. TotalProcessorTime - prevTime). TotalMilliseconds/2000/Environment. ProcessorCount
'count cycle is 2 seconds, so divided by 2000, meaning the cycle, the process how much CPU time percentage multiplied by 100 because in
'prevTime is the last time the statistics to proc. TotalProcessorTime, need for the results of two statistical difference to
Memory is more complex, proc. Workingset64 and performance counter workingset is not the same thing, doubt proc. Workingset64 + proc. Workingset PrivateMemorysize64 is the performance counter, this but I'm guessing, you want to fine pick can dig it,
Public Class Form1
Dim proc As Process
Dim prevTime As TimeSpan=TimeSpan. Zero
Private Sub Timer1_Tick (sender As Object, As EventArgs e) Handles Timer1. Tick
Label1. Text=the String. Format (
Private: "CPU: {0} % {1} MB Workingset: {2} MB",
100 * (proc. TotalProcessorTime - prevTime). TotalMilliseconds/2000/Environment. ProcessorCount,
(proc. PrivateMemorySize64 & gt;> 16). ToString (" # # # # #, # # 0. "),
(proc. WorkingSet64 & gt;> 16). ToString (" # # # # #, # # 0. "))
PrevTime=proc. TotalProcessorTime
End Sub
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
Proc=Process. GetCurrentProcess ()
End Sub
The End of the Class
Method 3: Management under the namespace, wmi query using ManagementObjectSearcher
Imports System. Management
Public Class Form1
Dim proc As Process
Private Sub Timer1_Tick (sender As Object, As EventArgs e) Handles Timer1. Tick
Dim a searcher As ManagementObjectSearcher=
New ManagementObjectSearcher (
"Select * from Win32_PerfFormattedData_PerfProc_Process where IDProcess=" & amp; Process. GetCurrentProcess (). Id)
Dim obj As ManagementObject=a searcher. The Get () Cast (Of ManagementObject). The First ()
Label1. Text=the String. Format (
Private: "CPU: {0} % {1} MB Workingset: {1} MB",
Obj (" PercentProcessorTime "),
The Convert. ToInt64 (obj (" PrivateBytes ") & gt;> 16). ToString (" # # # # #, # # 0. "),
The Convert. ToInt64 (obj (" WorkingSet ") & gt;> 16). ToString (" # # # # #, # # 0. "))
End Sub
The End of the Class
Methods four, direct use wmi query object, method and three is essentially the same, too lazy to write, online search a lot, this is VB6 s should can also be used in.net, literally searched a
https://zhidao.baidu.com/question/2078259333515194308.html
Memory is the most critical in these methods, it is multifarious, the same KEY name, completely different, don't have the energy to explore, you try it yourself,
CodePudding user response:
I tried, four CPU utilization were similar, the key is memory, task manager shows 14 MB, three methods are 300 +, why is this?CodePudding user response:
So the key question is, which one do you want to get the memory?There are a lot of memory related parameters, workingset (private and sharable), paged, PrivateBytes, virtual memory (private and sharable)...
https://blog.csdn.net/wzsy/article/details/42737907
This article explains the most type, if you want to fully understand, you need to take the task manager, you want to which a column corresponding to the actual parameters to find, search, more actual running a program test again, that I don't know, can't help you,
CodePudding user response:
Direct access to the data in the task manager,CodePudding user response:
There is a limit to the elder brothers, stretched out his hand,CodePudding user response:
,,,But I want to own a method to solve the,
Pf1=New PerformanceCounter (" Process ", "Working Set - Private", names)
CPU=cpucounter. NextValue ()
Label7. Text="CPU utilization (single kernel) :" & amp; Math. Round (CPU, 1) & amp; "%" & amp; VbCrLf & amp; "Memory usage:" & amp; Math. Round (pf1 NextValue ()/1024/1024, 2) & amp; "MB"