|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Diagnostics; |
4 | | -using System.Threading.Tasks; |
5 | | -using System.Timers; |
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Management; |
6 | 3 | using MemPlus.Classes.RAM.ViewModels; |
7 | 4 |
|
8 | 5 | namespace MemPlus.Classes.RAM |
9 | 6 | { |
10 | | - internal sealed class RamAnalyzer |
| 7 | + /// <summary> |
| 8 | + /// Static class that can be used to retrieve RAM information |
| 9 | + /// </summary> |
| 10 | + internal static class RamAnalyzer |
11 | 11 | { |
12 | | - private readonly List<ProcessData> _processDataList; |
13 | | - |
14 | | - internal delegate void ProcessAddedEvent(ProcessData processData); |
15 | | - internal delegate void ProcessRemovedEvent(ProcessData processData); |
16 | | - |
17 | | - private readonly ProcessAddedEvent _processAddedEvent; |
18 | | - private readonly ProcessRemovedEvent _processRemovedEvent; |
19 | | - |
20 | | - internal RamAnalyzer(int delay, ProcessAddedEvent processAddedEvent, ProcessRemovedEvent processRemovedEvent) |
| 12 | + /// <summary> |
| 13 | + /// Retrieve RAM information |
| 14 | + /// </summary> |
| 15 | + /// <returns>A list of RAM information</returns> |
| 16 | + internal static List<RamStick> GetRamSticks() |
21 | 17 | { |
22 | | - _processAddedEvent = processAddedEvent; |
23 | | - _processRemovedEvent = processRemovedEvent; |
| 18 | + List<RamStick> ramSticks = new List<RamStick>(); |
24 | 19 |
|
25 | | - _processDataList = new List<ProcessData>(); |
26 | | - Timer updateTimer = new Timer |
27 | | - { |
28 | | - Interval = delay, |
29 | | - Enabled = true |
30 | | - }; |
| 20 | + ConnectionOptions connection = new ConnectionOptions {Impersonation = ImpersonationLevel.Impersonate}; |
31 | 21 |
|
32 | | - updateTimer.Elapsed += UpdateTimerOnElapsed; |
33 | | - UpdateTimerOnElapsed(null, null); |
34 | | - } |
| 22 | + ManagementScope scope = new ManagementScope("\\root\\CIMV2", connection); |
| 23 | + scope.Connect(); |
35 | 24 |
|
36 | | - private async void UpdateTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) |
37 | | - { |
38 | | - List<string> currentPaths = new List<string>(); |
39 | | - await Task.Run(async () => |
40 | | - { |
41 | | - foreach (Process p in Process.GetProcesses()) |
42 | | - { |
43 | | - try |
44 | | - { |
45 | | - currentPaths.Add(p.MainModule.FileName); |
46 | | - |
47 | | - ProcessData processData = EqualsPath(p.MainModule.FileName); |
48 | | - bool addProcessData = false; |
49 | | - if (processData == null) |
50 | | - { |
51 | | - processData = new ProcessData(); |
52 | | - addProcessData = true; |
53 | | - } |
| 25 | + ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PhysicalMemory"); |
54 | 26 |
|
55 | | - processData.ProcessName = p.ProcessName; |
56 | | - processData.ProcessLocation = p.MainModule.FileName; |
57 | | - processData.Pid = p.Id; |
58 | | - processData.WorkingSet = (p.WorkingSet64 / 1024 / 1024).ToString("F2") + " MB"; |
| 27 | + ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); |
59 | 28 |
|
60 | | - if (addProcessData) |
61 | | - { |
62 | | - _processDataList.Add(processData); |
63 | | - _processAddedEvent.Invoke(processData); |
64 | | - } |
65 | | - } |
66 | | - catch (Exception ex) |
67 | | - { |
68 | | - |
69 | | - } |
70 | | - } |
71 | | - await CleanProcessList(currentPaths); |
72 | | - }); |
73 | | - } |
74 | | - |
75 | | - private async Task CleanProcessList(IReadOnlyCollection<string> currentPaths) |
76 | | - { |
77 | | - await Task.Run(() => |
| 29 | + // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop |
| 30 | + foreach (ManagementObject queryObj in searcher.Get()) |
78 | 31 | { |
79 | | - for (int i = _processDataList.Count - 1; i >= 0; i--) |
| 32 | + RamStick stick = new RamStick(); |
| 33 | + foreach (PropertyData data in queryObj.Properties) |
80 | 34 | { |
81 | | - bool remove = true; |
82 | | - |
83 | | - foreach (string s in currentPaths) |
84 | | - { |
85 | | - if (_processDataList[i].ProcessLocation == s) |
86 | | - { |
87 | | - remove = false; |
88 | | - break; |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - if (remove) |
| 35 | + if (data.Value != null) |
93 | 36 | { |
94 | | - _processRemovedEvent.Invoke(_processDataList[i]); |
95 | | - _processDataList.RemoveAt(i); |
| 37 | + stick.AddRamData(new RamData(data.Name, data.Value.ToString())); |
96 | 38 | } |
97 | 39 | } |
98 | | - }); |
99 | | - } |
100 | 40 |
|
101 | | - private ProcessData EqualsPath(string path) |
102 | | - { |
103 | | - foreach (ProcessData pd in _processDataList) |
104 | | - { |
105 | | - if (pd.ProcessLocation == path) |
106 | | - { |
107 | | - return pd; |
108 | | - } |
| 41 | + ramSticks.Add(stick); |
109 | 42 | } |
110 | | - return null; |
111 | | - } |
112 | 43 |
|
113 | | - internal List<ProcessData> GetProcessData() |
114 | | - { |
115 | | - return _processDataList; |
| 44 | + return ramSticks; |
116 | 45 | } |
117 | 46 | } |
118 | 47 | } |
0 commit comments