use timer

This commit is contained in:
pypy
2021-01-21 11:31:08 +09:00
parent 472e12e123
commit 4e2f214119

View File

@@ -15,7 +15,7 @@ namespace VRCX
private readonly ReaderWriterLockSlim m_Lock; private readonly ReaderWriterLockSlim m_Lock;
private readonly RichPresence m_Presence; private readonly RichPresence m_Presence;
private DiscordRpcClient m_Client; private DiscordRpcClient m_Client;
private Thread m_Thread; private Timer m_Timer;
private bool m_Active; private bool m_Active;
static Discord() static Discord()
@@ -27,40 +27,30 @@ namespace VRCX
{ {
m_Lock = new ReaderWriterLockSlim(); m_Lock = new ReaderWriterLockSlim();
m_Presence = new RichPresence(); m_Presence = new RichPresence();
m_Thread = new Thread(ThreadLoop) m_Timer = new Timer(TimerCallback, null, -1, -1);
{
IsBackground = true
};
} }
internal void Init() internal void Init()
{ {
m_Thread.Start(); m_Timer.Change(0, 1000);
} }
internal void Exit() internal void Exit()
{ {
var thread = m_Thread; m_Timer.Change(-1, -1);
m_Thread = null;
thread.Interrupt();
thread.Join();
m_Client?.Dispose(); m_Client?.Dispose();
} }
private void ThreadLoop() private void TimerCallback(object state)
{ {
while (m_Thread != null) lock (this)
{ {
Update();
try try
{ {
Thread.Sleep(1000); Update();
} }
catch catch
{ {
// ThreadInterruptedException
} }
} }
} }