From eeb106c4896fa13cb8bbf8e395d9782308c8dac2 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Fri, 11 Apr 2025 13:45:04 -0500 Subject: [PATCH] fix: use semaphore for thread safety in Start method --- .../Monitoring/GenericCommunicationMonitor.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/PepperDash.Essentials.Core/Monitoring/GenericCommunicationMonitor.cs b/src/PepperDash.Essentials.Core/Monitoring/GenericCommunicationMonitor.cs index d012ff2c..a3a427f1 100644 --- a/src/PepperDash.Essentials.Core/Monitoring/GenericCommunicationMonitor.cs +++ b/src/PepperDash.Essentials.Core/Monitoring/GenericCommunicationMonitor.cs @@ -30,6 +30,8 @@ namespace PepperDash.Essentials.Core private Timer PollTimer; + private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1); + /// /// GenericCommunicationMonitor constructor /// @@ -201,14 +203,21 @@ namespace PepperDash.Essentials.Core private void BeginPolling() { - lock (_pollTimerLock) + try { - if (PollTimer != null) + semaphore.Wait(); { - return; - } + if (PollTimer != null) + { + return; + } - PollTimer = new Timer(o => Poll(), null, 0, PollTime); + PollTimer = new Timer(o => Poll(), null, 0, PollTime); + } + } + finally + { + semaphore.Release(); } }