设备线程内使用ManualResetEvent

This commit is contained in:
iioter 2023-10-21 22:14:18 +08:00
parent 4152b8c598
commit 64e950c52b

View File

@ -23,7 +23,7 @@ namespace Plugin
private Task? _task;
private readonly DateTime _tsStartDt = new(1970, 1, 1);
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
private readonly object _lock = new();
private ManualResetEvent resetEvent = new(true);
public DeviceThread(Device device, IDriver driver, string projectId, MyMqttClient myMqttClient,
MqttServer mqttServer, ILogger logger)
@ -68,8 +68,7 @@ namespace Plugin
return;
}
lock (_lock)
{
resetEvent.WaitOne();
try
{
if (driver.IsConnected)
@ -224,7 +223,6 @@ namespace Plugin
{
_logger.LogError(ex, $"线程循环异常,{Device.DeviceName}");
}
}
Thread.Sleep(Device.DeviceVariables!.Any() ? (int)Driver.MinPeriod : 10000);
@ -255,8 +253,8 @@ namespace Plugin
//执行写入变量RPC
if (e.Method.ToLower() == "write")
{
lock (_lock)
{
resetEvent.Reset();
bool rpcConnected = false;
//没连接就连接
if (!Driver.IsConnected)
@ -309,7 +307,7 @@ namespace Plugin
rpcResponse.IsSuccess = false;
rpcResponse.Description = $"{e.DeviceName} 连接失败";
}
}
resetEvent.Set();
}
//其他RPC TODO
else