1 | #Region "Microsoft.VisualBasic::9ca8ed81894ce206c453ea25b3ab4e48, Microsoft.VisualBasic.Core\ApplicationServices\Tools\Win32\TaskManager.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module TaskManager |
35 | ' |
36 | ' Properties: CPU_Usages |
37 | ' |
38 | ' Function: ProcessUsage, ProcessUsageDetails |
39 | ' Structure TaskInfo |
40 | ' |
41 | ' Properties: CommandLine, CPU, Memory, PID, ProcessName |
42 | ' |
43 | ' Function: ToString |
44 | ' |
45 | ' |
46 | ' |
47 | ' |
48 | ' /********************************************************************************/ |
49 | |
50 | #End Region |
51 | |
52 | Imports System.Xml.Serialization |
53 | Imports Microsoft.VisualBasic.Language |
54 | Imports Microsoft.VisualBasic.Serialization.JSON |
55 | Imports sys = System.Math |
56 | |
57 | Namespace Win32 |
58 | |
59 | ''' <summary> |
60 | ''' Windows的任务管理器的接口 |
61 | ''' </summary> |
62 | Public Module TaskManager |
63 | |
64 | ''' <summary> |
65 | ''' Using this property you can display the CPU usage (over all CPU usage like you would find on the task manager) |
66 | ''' </summary> |
67 | ''' <returns></returns> |
68 | Public ReadOnly Property CPU_Usages As New PerformanceCounter("Processor", "% Processor Time", "_Total") |
69 | |
70 | ''' <summary> |
71 | ''' 类似于任务管理器的函数:Memory, CPU, ProcessName, PID, CommandLine |
72 | ''' </summary> |
73 | ''' <returns>Memory, CPU</returns> |
74 | ''' <remarks></remarks> |
75 | Public Function ProcessUsageDetails() As List(Of TaskInfo) |
76 | Dim counterList As New List(Of TaskInfo) |
77 | |
78 | Try |
79 | Dim proc As Process() = Process.GetProcesses |
80 | |
81 | For Each P As Process In proc |
82 | Dim pCounter As New PerformanceCounter("Process", "% Processor Time", P.ProcessName) |
83 | |
84 | counterList += New TaskInfo With { |
85 | .Memory = P.WorkingSet64, |
86 | .CPU = sys.Round(pCounter.NextValue, 2), |
87 | .ProcessName = P.ProcessName, |
88 | .PID = P.Id, |
89 | .CommandLine = P.StartInfo.FileName & " " & P.StartInfo.Arguments |
90 | } |
91 | Next |
92 | Catch ex As Exception |
93 | Call App.LogException(ex) |
94 | Call ex.PrintException |
95 | End Try |
96 | |
97 | Return counterList |
98 | End Function |
99 | |
100 | Public Structure TaskInfo |
101 | |
102 | <XmlAttribute> Public Property PID As Integer |
103 | <XmlAttribute> Public Property CommandLine As String |
104 | <XmlAttribute> Public Property ProcessName As String |
105 | <XmlAttribute> Public Property CPU As Double |
106 | <XmlAttribute> Public Property Memory As Long |
107 | |
108 | Default Public ReadOnly Property GetValue(name$) As Object |
109 | Get |
110 | Select Case name |
111 | Case NameOf(PID) |
112 | Return PID |
113 | |
114 | Case NameOf(CommandLine) |
115 | Return CommandLine |
116 | |
117 | Case NameOf(ProcessName) |
118 | Return ProcessName |
119 | |
120 | Case NameOf(CPU) |
121 | Return CPU |
122 | |
123 | Case NameOf(Memory) |
124 | Return Memory |
125 | |
126 | Case Else |
127 | Throw New Exception($"Unable found key '{name}' in: " & GetJson) |
128 | End Select |
129 | End Get |
130 | End Property |
131 | |
132 | Public Overrides Function ToString() As String |
133 | Return Me.GetJson |
134 | End Function |
135 | End Structure |
136 | |
137 | ''' <summary> |
138 | ''' 获取CPU的使用率 |
139 | ''' </summary> |
140 | ''' <returns></returns> |
141 | ''' <remarks></remarks> |
142 | Public Function ProcessUsage() As Double |
143 | Dim tasks As List(Of TaskInfo) = ProcessUsageDetails() |
144 | Dim usage As Double = tasks _ |
145 | .Sum(Function(proc) CType(proc("CPU"), Double)) |
146 | Return usage |
147 | End Function |
148 | End Module |
149 | End Namespace |