From 0aad91338e0ab1e36b150bf59387cedc270cc0e4 Mon Sep 17 00:00:00 2001 From: luosheng Date: Fri, 14 Jul 2023 09:16:49 +0800 Subject: [PATCH] Fix --- .../Modbus.Net/Controller/FifoController.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Modbus.Net/Modbus.Net/Controller/FifoController.cs b/Modbus.Net/Modbus.Net/Controller/FifoController.cs index 72efbb3..096b458 100644 --- a/Modbus.Net/Modbus.Net/Controller/FifoController.cs +++ b/Modbus.Net/Modbus.Net/Controller/FifoController.cs @@ -16,9 +16,11 @@ namespace Modbus.Net private bool _taskCancel = false; + private bool _activeSema = false; + private int _waitingListMaxCount; - private readonly Semaphore _taskCycleSema; + private Semaphore _taskCycleSema; /// /// 间隔时间 @@ -37,16 +39,17 @@ namespace Modbus.Net : base(lengthCalc, checkRightFunc) { _waitingListMaxCount = int.Parse(waitingListMaxCount != null ? waitingListMaxCount.ToString() : null ?? ConfigurationReader.GetValueDirect("Controller", "WaitingListCount")); - if (activateSema) - { - _taskCycleSema = new Semaphore(0, _waitingListMaxCount); - } + _activeSema = activateSema; AcquireTime = acquireTime; } /// protected override void SendingMessageControlInner() { + if (_activeSema) + { + _taskCycleSema = new Semaphore(0, _waitingListMaxCount); + } _taskCycleSema?.WaitOne(); while (!_taskCancel) { @@ -101,6 +104,9 @@ namespace Modbus.Net _taskCycleSema?.WaitOne(); } } + _taskCycleSema.Dispose(); + _taskCycleSema = null; + Clear(); } ///