1
0
mirror of https://github.com/DarthAffe/OBD.NET.git synced 2025-12-12 08:48:30 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
35af0f8c15 Merge branch 'fix/deadlock' 2021-11-03 23:35:43 +01:00
1419ed7755
Merge pull request #24 from MB512/fix/deadlock
Fix deadlock on connection lost
2021-11-03 23:35:10 +01:00
b007471ed6 Refactored deadlock fix 2021-11-03 23:31:57 +01:00
Malte Bitter
34940632a3 Fix deadlock on connection lost 2021-09-16 11:39:34 +02:00

View File

@ -197,6 +197,8 @@ namespace OBD.NET.Common.Devices
/// </summary>
private async void CommandWorker()
{
CancellationToken cancellationToken = _commandCancellationToken.Token;
while (!_commandCancellationToken.IsCancellationRequested)
{
CurrentCommand = null;
@ -206,7 +208,7 @@ namespace OBD.NET.Common.Devices
try
{
if (_commandQueue.TryTake(out CurrentCommand, 10, _commandCancellationToken.Token))
if (_commandQueue.TryTake(out CurrentCommand, 10, cancellationToken))
{
_queueSize--;
@ -217,12 +219,19 @@ namespace OBD.NET.Common.Devices
else
Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
//wait for command to finish
_commandFinishedEvent.WaitOne();
// wait for command to finish or command canceled
while (!_commandFinishedEvent.WaitOne(50))
cancellationToken.ThrowIfCancellationRequested();
}
}
catch (OperationCanceledException) { /*ignore, because it is thrown when the cancellation token is canceled*/}
catch (OperationCanceledException)
{
CurrentCommand?.CommandResult.WaitHandle.Set();
}
}
foreach (QueuedCommand cmd in _commandQueue)
cmd.CommandResult.WaitHandle.Set();
}
public void WaitQueue() => _queueEmptyEvent.WaitOne();