mirror of
https://github.com/DarthAffe/OBD.NET.git
synced 2025-12-13 09:18:31 +00:00
Merge pull request #4 from romanlum/OperationCanceledException-fix
this fixes #3
This commit is contained in:
commit
34f665628c
@ -192,19 +192,23 @@ 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*/}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user