Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Win/WMI/WMI Process CPU usage
Function:
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Win/WMI/WMI Process CPU usage
This example is the version from Sun, 3rd Jun 2017.
Project "WMI Process CPU usage.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // required for Windows 8.1 and newer call WindowsWMIMBS.InitSecurity(false) End EventHandler
End Class
Class Window1 Inherits Window
Const kColumnName = 1
Const kColumnPMemory = 2
Const kColumnTime = 4
Const kColumnVMemory = 3
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control UpdateTimer Inherits Timer
ControlInstance UpdateTimer Inherits Timer
EventHandler Sub Action() update End EventHandler
End Control
EventHandler Sub Open() w=new WindowsWMIMBS if w.ConnectServer("root\cimv2") then 'MsgBox "Connected" else MsgBox "ConnectServer: fail"+EndOfLine+"Lasterror: "+str(w.Lasterror) end if list.ColumnAlignment(kColumnPMemory) = List.AlignRight list.ColumnAlignment(kColumnVMemory) = List.AlignRight list.ColumnAlignment(kColumnTime) = List.AlignRight End EventHandler
Function RowForID(ID as integer, name as string) As integer dim u as integer = List.ListCount-1 for i as integer = 0 to u if list.RowTag(i) = ID and list.cell(i, kColumnName) = name then Return i end if next Return -1 End Function
Function formatMemory(n as int64) As string if n < 2* 1024 then Return str(n) elseif n < 2 * 1024 * 1024 then return str(n \ 1024)+" KB" else return str(n \ 1024 \ 1024)+" MB" end if End Function
Sub update() if w.query("WQL", "SELECT IDProcess, Name, VirtualBytes, PrivateBytes, PercentProcessorTime FROM Win32_PerfRawData_PerfProc_Process") then dim stats() as Stats dim TotalPercentProcessorTime as Int64 while w.NextItem dim s as new stats s.name = w.getProperty("Name") s.ID = w.GetPropertyInteger("IDProcess") s.VirtualBytes = w.GetPropertyInt64("VirtualBytes") s.PrivateBytes = w.GetPropertyInt64("PrivateBytes") s.PercentProcessorTime = w.GetPropertyInt64("PercentProcessorTime") stats.Append s if s.Name = "_total" then TotalPercentProcessorTime = s.PercentProcessorTime end if wend dim deltaTotalPercentProcessorTime as int64 = LastTotalPercentProcessorTime - TotalPercentProcessorTime for each s as stats in stats dim row as integer = RowForID(s.ID, s.name) if row = -1 then list.AddRow str(s.ID), "", "", "", "" row = list.LastIndex list.RowTag(row) = s.ID list.CellTag(row, kColumnTime) = s.PercentProcessorTime else dim p as int64 = list.CellTag(row, kColumnTime) dim d as int64 = s.PercentProcessorTime-p list.cell(row, kColumnTime) = format(d / DeltaTotalPercentProcessorTime,"0.0%") list.CellTag(row, kColumnTime) = s.PercentProcessorTime end if list.Cell(row, kColumnName) = s.name list.cell(row, kColumnPMemory) = formatMemory(s.PrivateBytes) list.cell(row, kColumnVMemory) = formatMemory(s.VirtualBytes) next LastTotalPercentProcessorTime = TotalPercentProcessorTime else MsgBox "query: fail"+EndOfLine+"Lasterror: "+str(w.Lasterror) end if End Sub
Property LastTotalPercentProcessorTime As INt64
Property w As WindowsWMIMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class Stats
Property ID As Integer
Property Name As string
Property PercentProcessorTime As Int64
Property PrivateBytes As Int64
Property VirtualBytes As Int64
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Win Plugin.




Links
MBS Xojo Plugins