ObserveUseage.cs (5119B)
1 using Microsoft.Extensions.Configuration; 2 using Microsoft.Toolkit.Uwp.Notifications; 3 using System.Diagnostics; 4 using System.Runtime.InteropServices; 5 6 namespace BusynessNotification 7 { 8 internal class ObserveUseage 9 { 10 11 12 public async void Controller() 13 { 14 var configuration = new ConfigurationBuilder() 15 .AddJsonFile("SettingJson.json") 16 .Build(); 17 Debug.WriteLine("Controller has colled"); 18 ///設定から数値を取得 19 bool CheckCPU = Convert.ToBoolean(configuration["CheckCPU"]); 20 bool CheckMemory = Convert.ToBoolean(configuration["CheckMemory"]); 21 bool CheckDisk = Convert.ToBoolean(configuration["CheckDisk"]); 22 23 double CPUSlider = Convert.ToDouble(configuration["SliderCPU"]); 24 double MemorySlider = Convert.ToDouble(configuration["SliderMemory"]); 25 double DiskSlider = Convert.ToDouble(configuration["SliderDisk"]); 26 27 double SecCPU = Convert.ToDouble(configuration["SecCPU"]); 28 double SecMemory = Convert.ToDouble(configuration["SecMemory"]); 29 double SecDisk = Convert.ToDouble(configuration["SecDisk"]); 30 31 int flagCPU = 0; 32 int flagMemory = 0; 33 int flagDisk = 0; 34 35 Debug.WriteLine("Setting file have readed"); 36 37 Debug.WriteLine("CheckCPU=" + CheckCPU + " CheckMemory=" + CheckMemory + " CheckDisk=" + CheckDisk); 38 Debug.WriteLine("SliderCPU=" + CPUSlider + " SliderMemory=" + MemorySlider + " SliderDisk=" + DiskSlider); 39 Debug.WriteLine("SecCPU=" + SecCPU + " SecMemory=" + SecMemory + " SecDisk=" + SecDisk); 40 41 ///インスタンス 42 System.Diagnostics.PerformanceCounter cpuCounter = new("Processor Information", "% Processor Utility", "_Total"); 43 44 PerformanceCounter performanceCounterRAM = new() 45 { 46 CounterName = "% Committed Bytes In Use", 47 CategoryName = "Memory" 48 }; 49 50 PerformanceCounter Disc1 = new() 51 { 52 CategoryName = "LogicalDisk", 53 CounterName = "Disk Transfers/sec", 54 InstanceName = "C:" 55 }; 56 57 System.Threading.Timer timer = null; 58 59 async void timer_delegate(object? state) 60 { 61 62 Debug.WriteLine("timer is still waiking"); 63 64 if (CheckCPU) 65 { 66 await Task.Run(() => 67 { 68 if (cpuCounter.NextValue() <= CPUSlider) 69 { 70 flagCPU++; 71 72 Debug.WriteLine("CPU flag" + flagCPU); 73 74 } 75 else 76 { 77 flagCPU = 0; 78 } 79 }); 80 } 81 if (CheckMemory) 82 { 83 await Task.Run(() => 84 { 85 if (performanceCounterRAM.NextValue() <= MemorySlider) 86 { 87 flagMemory++; 88 89 Debug.WriteLine("RAM flag" + flagMemory); 90 91 } 92 else 93 { 94 flagMemory = 0; 95 } 96 }); 97 } 98 if (CheckDisk) 99 { 100 await Task.Run(() => 101 { 102 if (Disc1.NextValue() <= DiskSlider) 103 { 104 flagDisk++; 105 106 Debug.WriteLine("Disk flag" + flagDisk); 107 108 } 109 else 110 { 111 flagDisk = 0; 112 } 113 }); 114 } 115 116 117 if (flagCPU >= SecCPU && flagMemory >= SecMemory && flagDisk >= SecDisk) 118 { 119 Debug.WriteLine("All clear"); 120 121 timer.Dispose(); 122 123 new ToastContentBuilder() 124 .AddText("Byssyness Notification") 125 .AddText("This PC is now available") 126 .Show(); 127 128 //check BN View is up 129 [DllImport("user32.dll")] 130 static extern IntPtr GetForegroundWindow(); 131 132 [DllImport("user32.dll")] 133 static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); 134 135 _ = GetWindowThreadProcessId(GetForegroundWindow(), out int processid); 136 Process p = Process.GetProcessById(processid); 137 if (p.MainModule.FileVersionInfo.ProductName != "BusynessNotification") 138 { 139 Environment.Exit(0); 140 } 141 } 142 } 143 timer = new System.Threading.Timer(timer_delegate, null, 1000, 1000); 144 } 145 } 146 }