mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-06 22:46:06 +02:00
use timer
This commit is contained in:
+9
-21
@@ -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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user