mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-12 08:48:30 +00:00
Refactored deadlock fix
This commit is contained in:
parent
34940632a3
commit
b007471ed6
@ -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,24 +219,19 @@ namespace OBD.NET.Common.Devices
|
||||
else
|
||||
Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText));
|
||||
|
||||
//wait for command to finish or command canceled
|
||||
while (!(_commandFinishedEvent.WaitOne(50) || _commandCancellationToken.IsCancellationRequested))
|
||||
{
|
||||
}
|
||||
// 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*/}
|
||||
|
||||
// if canceled set all commands as completed (with null result)
|
||||
if (_commandCancellationToken.IsCancellationRequested)
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
CurrentCommand?.CommandResult.WaitHandle.Set();
|
||||
foreach (var cmd in _commandQueue)
|
||||
{
|
||||
cmd.CommandResult.WaitHandle.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (QueuedCommand cmd in _commandQueue)
|
||||
cmd.CommandResult.WaitHandle.Set();
|
||||
}
|
||||
|
||||
public void WaitQueue() => _queueEmptyEvent.WaitOne();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user