Skip to content

Commit 3a23306

Browse files
author
Martin Lercher
committed
Better disposal
1 parent a147d82 commit 3a23306

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/JsonRpc/OutputHandler.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace JsonRpc
1010
public class OutputHandler : IOutputHandler
1111
{
1212
private readonly TextWriter _output;
13-
private Thread _thread;
13+
private readonly Thread _thread;
1414
private readonly BlockingCollection<object> _queue;
1515
private readonly CancellationTokenSource _cancel;
1616

@@ -41,7 +41,6 @@ private void ProcessOutputQueue()
4141
{
4242
while (true)
4343
{
44-
if (_thread == null) return;
4544
if (_queue.TryTake(out var value, Timeout.Infinite, token))
4645
{
4746
var content = JsonConvert.SerializeObject(value);
@@ -51,7 +50,6 @@ private void ProcessOutputQueue()
5150
sb.Append($"Content-Length: {content.Length}\r\n");
5251
sb.Append($"\r\n");
5352
sb.Append(content);
54-
5553
_output.Write(sb.ToString());
5654
}
5755
}
@@ -62,14 +60,14 @@ private void ProcessOutputQueue()
6260
throw;
6361
// else ignore. Exceptions: OperationCanceledException - The CancellationToken has been canceled.
6462
}
65-
finally { _cancel.Dispose(); }
6663
}
6764

6865
public void Dispose()
6966
{
70-
_output?.Dispose();
71-
_thread = null;
7267
_cancel.Cancel();
68+
_thread.Join();
69+
_cancel.Dispose();
70+
_output.Dispose();
7371
}
7472
}
7573
}

0 commit comments

Comments
 (0)