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

Merge pull request #4 from romanlum/OperationCanceledException-fix

this fixes #3
This commit is contained in:
DarthAffe 2017-08-18 18:03:50 +02:00 committed by GitHub
commit 34f665628c

View File

@ -192,20 +192,24 @@ namespace OBD.NET.Common.Devices
{ {
while (!_commandCancellationToken.IsCancellationRequested) while (!_commandCancellationToken.IsCancellationRequested)
{ {
CurrentCommand = null; currentCommand = null;
if (_commandQueue.TryTake(out CurrentCommand, Timeout.Infinite, _commandCancellationToken.Token)) try
{ {
Logger?.WriteLine("Writing Command: '" + CurrentCommand.CommandText.Replace('\r', '\'') + "'", OBDLogLevel.Verbose); if (commandQueue.TryTake(out currentCommand, Timeout.Infinite, commandCancellationToken.Token))
{
Logger?.WriteLine("Writing Command: '" + currentCommand.CommandText.Replace('\r', '\'') + "'", OBDLogLevel.Verbose);
if (Connection.IsAsync) if (Connection.IsAsync)
await Connection.WriteAsync(Encoding.ASCII.GetBytes(CurrentCommand.CommandText)); await Connection.WriteAsync(Encoding.ASCII.GetBytes(currentCommand.CommandText));
else else
Connection.Write(Encoding.ASCII.GetBytes(CurrentCommand.CommandText)); Connection.Write(Encoding.ASCII.GetBytes(currentCommand.CommandText));
//wait for command to finish //wait for command to finish
_commandFinishedEvent.WaitOne(); commandFinishedEvent.WaitOne();
} }
} }
catch (OperationCanceledException)
{/*ignore, because it is thrown when the cancellation token is canceled*/}
}
} }
/// <summary> /// <summary>
@ -213,9 +217,9 @@ namespace OBD.NET.Common.Devices
/// </summary> /// </summary>
public virtual void Dispose() public virtual void Dispose()
{ {
_commandCancellationToken?.Cancel(); commandQueue.CompleteAdding();
_commandWorkerTask?.Wait(); commandCancellationToken?.Cancel();
commandWorkerTask?.Wait();
Connection?.Dispose(); Connection?.Dispose();
} }