SteamController: Start controller with delay after Resume

This commit is contained in:
Kamil Trzciński 2023-01-05 11:47:18 +01:00
parent edca1663c8
commit c05f8f2f22
3 changed files with 58 additions and 5 deletions

View file

@ -12,24 +12,32 @@ namespace SteamController
public int UpdatesPerSec { get; private set; }
public bool Start()
public bool Start(int? startDelayMs = null)
{
if (thread is not null)
return false;
UpdatesPerSec = 0;
threadRunning = true;
thread = new Thread(ThreadLoop);
thread.Start();
thread.Start(startDelayMs);
return true;
}
private void ThreadLoop(object? obj)
private void ThreadLoop(object? startDelayMs)
{
if (startDelayMs is int)
{
ThreadSleep((int)startDelayMs);
}
var stopwatch = new Stopwatch();
stopwatch.Start();
int updates = 0;
var nextReset = stopwatch.Elapsed.Add(UpdateResetInterval);
X360.Start();
while (threadRunning)
{
if (nextReset < stopwatch.Elapsed)
@ -45,10 +53,11 @@ namespace SteamController
if (!Enabled || !Steam.Updated)
{
try { Thread.Sleep(100); }
catch (ThreadInterruptedException) { }
ThreadSleep(100);
}
}
X360.Stop();
}
public void Stop()
@ -62,5 +71,18 @@ namespace SteamController
thread = null;
}
}
private bool ThreadSleep(int delayMs)
{
try
{
Thread.Sleep(delayMs);
return true;
}
catch (ThreadInterruptedException)
{
return false;
}
}
}
}