在任务管理器中获取CPU时间 [英] Getting CPU Time like in the task manager
本文介绍了在任务管理器中获取CPU时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图像在任务管理器中那样获取CPU时间,但是我得到的值和进程与任务管理器中显示的内容不匹配.
这是我正在使用的代码,
I am trying to get the CPU time like in the task manager but the values and processes i am getting are not matching what is displayed in the task manager.
This is the code i am using,
Public Shared Function getAllProcesses() As List(Of ServProcess)
Dim procLST As New List(Of ServProcess)
Dim ProcID As Integer
Dim ret As New ServProcess
Try
Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_Process")
Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
Dim m_Process As New ServProcess
m_Process.Name = Mgmt("Name")
ProcID = Mgmt("ProcessID")
If ProcID <> 0 Then
ret = GetUssage(ProcID)
m_Process.ProcessTime = ret.ProcessTime
m_Process.CpuVal = ret.CpuVal
If m_Process.ProcessTime IsNot Nothing And m_Process.Name IsNot Nothing And m_Process.CpuVal IsNot Nothing Then
If m_Process.ProcessTime <> "" Then
procLST.Add(m_Process)
End If
End If
End If
Next
Catch ex As Exception
End Try
Return procLST
End Function
Public Shared Function GetUssage(ByVal pid As String) As ServProcess
'get the process
Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_Process WHERE ProcessID = " + pid)
Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
Dim ProcessorUsage As Decimal
Dim msPassed As Decimal
Dim ret As New ServProcess
For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
Dim currentTime As DateTime = DateTime.Now
Try
Dim firstSample, secondSample As DateTime
Dim ProcessorTime As String
firstSample = DateTime.Now
Mgmt.Get()
Dim u_OldCPU As Long = CType(Mgmt.Properties("UserModeTime").Value, ULong) + CType(Mgmt.Properties("KernelModeTime").Value, ULong)
secondSample = DateTime.Now
Mgmt.Get()
Dim u_newCPU As ULong = CType(Mgmt.Properties("UserModeTime").Value, ULong) + CType(Mgmt.Properties("KernelModeTime").Value, ULong)
msPassed = CType((secondSample - firstSample).TotalMilliseconds, Decimal)
ProcessorUsage = CType(u_newCPU - u_OldCPU, Decimal) / (msPassed * 100 * Environment.ProcessorCount)
Dim mDate As DateTime = CDate(Mid(Mgmt("CreationDate"), 5, 2) & "/" & Mid(Mgmt("CreationDate"), 7, 2) & "/" & Left(Mgmt("CreationDate"), 4) & " " & Mid(Mgmt("CreationDate"), 9, 2) & ":" & Mid(Mgmt("CreationDate"), 11, 2) & ":" & Mid(Mgmt("CreationDate"), 13, 2))
Dim ProcessorSpan As TimeSpan = currentTime.Subtract(mDate)
ProcessorTime = ProcessorSpan.Days.ToString("000") & ":" & ProcessorSpan.Hours.ToString("00") & ":" & ProcessorSpan.Minutes.ToString("00")
ret.Name = ""
ret.CpuVal = ProcessorUsage.ToString("0.00")
ret.ProcessTime = ProcessorTime
Catch ex As Exception
End Try
Next
Return ret
End Function
我不确定是否要以错误的方式进行操作,或者是否有更好的更有效的方法来执行此操作.
所以我想我的问题是获取这些值以在Windows窗体的数据网格视图中显示它们的最佳方法是什么.
感谢
I am not sure if i am going about this the wrong way or if there is a better more efficient way to do this.
So I guess my question is what is the best way to get these values to display them in a datagrid view in a windows form.
thanks
推荐答案
这是查询CPU使用率所需要的:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/aa394277%28v=vs.85%29.aspx [ http://bit.ly/QMh4rI [ ^ ].
只是一个相关的代码示例:
http://social.msdn.microsoft.com /Forums/zh-CN/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/ [ http://social.msdn.microsoft.com /Forums/zh-CN/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/ [ http://www.csharphelp.com/2006/10/wmi-made-easy -for-c/ [ ^ ].
祝你好运,
—SA
This is what you will need to query CPU usage:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394277%28v=vs.85%29.aspx[^].
There is a number of code samples with some cookbook recipes for WMI query which you can easily find if you Google; for example:
http://bit.ly/QMh4rI[^].
Just one relevant code sample:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].
And this is the CodeProject article on the topic:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].
Nice and short introductory article:
http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/[^].
Good luck,
—SA
这就是我最终要做的事情
This is what i ended up doing
Public Shared Function getAllProcesses() As List(Of ServProcess)
Dim procLST As New List(Of ServProcess)
Dim mprocess As Array
mprocess = Process.GetProcesses()
Try
For Each p As Process In mprocess
Dim mProc As New ServProcess
Dim processorTimeCounter As PerformanceCounter = New PerformanceCounter("Process", "% Processor Time", p.ProcessName)
Try
processorTimeCounter.NextValue()
Threading.Thread.Sleep(500)
Dim ts As TimeSpan = p.TotalProcessorTime
Dim dt As DateTime = DateTime.MinValue.Add(ts)
Dim s As String
s = dt.ToString("HH:mm:ss") ' custom DateTime formatting
mProc.Name = p.ProcessName
mProc.CpuVal = processorTimeCounter.NextValue.ToString("0:00") + "%"
mProc.ProcessTime = s
If mProc.ProcessTime = "00:00:00" Then
Else
procLST.Add(mProc)
End If
Catch ex As Exception
End Try
Next
procLST.Sort(Function(x, y) y.ProcessTime.CompareTo(x.ProcessTime))
Return procLST
Catch ex As Exception
End Try
Return procLST
End Function
这篇关于在任务管理器中获取CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文