diff --git a/SmRtAPI/SmRtAPI/MessageWriter.cs b/SmRtAPI/SmRtAPI/MessageWriter.cs index 12ace3c..2733272 100644 --- a/SmRtAPI/SmRtAPI/MessageWriter.cs +++ b/SmRtAPI/SmRtAPI/MessageWriter.cs @@ -47,7 +47,7 @@ public async Task Start() var streamBuffer = new byte[_api.Configuration.BlockSize]; int bytesRead; - while ((bytesRead = await _stream.ReadAsync(streamBuffer, 0, streamBuffer.Length)) > 0 && !_transcriptionComplete.WaitOne(0)) + while ((bytesRead = await _stream.ReadAsync(streamBuffer, 0, streamBuffer.Length, _api.CancelToken)) > 0 && !_transcriptionComplete.WaitOne(0)) { await SendData(new ArraySegment(streamBuffer, 0, bytesRead)); _sequenceNumber++; diff --git a/SmRtAPI/SmRtAPI/SmRtApi.cs b/SmRtAPI/SmRtAPI/SmRtApi.cs index 97822e2..07d4220 100644 --- a/SmRtAPI/SmRtAPI/SmRtApi.cs +++ b/SmRtAPI/SmRtAPI/SmRtApi.cs @@ -148,16 +148,32 @@ public async Task RunAsync() /* The reading loop */ var t1 = Task.Factory.StartNew(async () => { - var reader = new MessageReader(this, wsClient, transcriptionComplete, recognitionStarted); - await reader.Start(); + try + { + var reader = new MessageReader(this, wsClient, transcriptionComplete, recognitionStarted); + await reader.Start(); + } + catch (OperationCanceledException) + { + transcriptionComplete.Set(); + } + }, CancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); /* The writing loop */ var t2 = Task.Factory.StartNew(async () => { - _writer = new MessageWriter(this, wsClient, transcriptionComplete, _stream, recognitionStarted); - await _writer.Start(); + try + { + _writer = new MessageWriter(this, wsClient, transcriptionComplete, _stream, recognitionStarted); + await _writer.Start(); + } + catch (OperationCanceledException) + { + transcriptionComplete.Set(); + } + }, CancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); transcriptionComplete.WaitOne();