use timer

This commit is contained in:
pypy
2021-01-21 11:27:28 +09:00
parent 32d5e12d34
commit 472e12e123
+9 -21
View File
@@ -13,7 +13,7 @@ namespace VRCX
public static readonly CpuMonitor Instance; public static readonly CpuMonitor Instance;
public float CpuUsage; public float CpuUsage;
private PerformanceCounter _performanceCounter; private PerformanceCounter _performanceCounter;
private Thread _thread; private Timer _timer;
static CpuMonitor() static CpuMonitor()
{ {
@@ -52,43 +52,31 @@ namespace VRCX
} }
} }
_thread = new Thread(ThreadLoop) _timer = new Timer(TimerCallback, null, -1, -1);
{
IsBackground = true
};
} }
internal void Init() internal void Init()
{ {
_thread.Start(); _timer.Change(1000, 1000);
} }
internal void Exit() internal void Exit()
{ {
var thread = _thread; _timer.Change(-1, -1);
_thread = null;
thread.Interrupt();
thread.Join();
_performanceCounter?.Dispose(); _performanceCounter?.Dispose();
} }
private void ThreadLoop() private void TimerCallback(object state)
{ {
while (_thread != null) try
{ {
if (_performanceCounter != null) if (_performanceCounter != null)
{ {
CpuUsage = _performanceCounter.NextValue(); CpuUsage = _performanceCounter.NextValue();
} }
}
try catch
{ {
Thread.Sleep(1000);
}
catch
{
// ThreadInterruptedException
}
} }
} }
} }