fix: use semaphore for thread safety in Start method

This commit is contained in:
Andrew Welker
2025-04-11 13:45:04 -05:00
parent c1d62ea5d4
commit eeb106c489

View File

@@ -30,6 +30,8 @@ namespace PepperDash.Essentials.Core
private Timer PollTimer;
private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
/// <summary>
/// GenericCommunicationMonitor constructor
///
@@ -201,7 +203,9 @@ namespace PepperDash.Essentials.Core
private void BeginPolling()
{
lock (_pollTimerLock)
try
{
semaphore.Wait();
{
if (PollTimer != null)
{
@@ -211,6 +215,11 @@ namespace PepperDash.Essentials.Core
PollTimer = new Timer(o => Poll(), null, 0, PollTime);
}
}
finally
{
semaphore.Release();
}
}
/// <summary>
/// Stop the poll cycle