-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #389 from OmniSharp/troy/sharedtestwriter
Update SharedTextWriter
- Loading branch information
Showing
1 changed file
with
17 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
using System.IO; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OmniSharp.Stdio.Services | ||
{ | ||
public class SharedTextWriter : ISharedTextWriter | ||
{ | ||
private BlockingCollection<object> _queue = new BlockingCollection<object>(); | ||
|
||
private readonly object _lock = new object(); | ||
private readonly TextWriter _writer; | ||
|
||
public SharedTextWriter(TextWriter writer) | ||
{ | ||
_writer = writer; | ||
|
||
var thread = new Thread(() => { while (true) WriteLine(_queue.Take()); }) | ||
{ | ||
Name = $"{nameof(SharedTextWriter)} {nameof(BlockingCollection<object>)}", | ||
IsBackground = true | ||
}; | ||
|
||
thread.Start(); | ||
} | ||
|
||
public void WriteLine(object value) | ||
{ | ||
lock(_lock) | ||
lock (_lock) | ||
{ | ||
_writer.WriteLine(value); | ||
} | ||
} | ||
|
||
public Task WriteLineAsync(object value) | ||
{ | ||
return Task.Factory.StartNew(() => WriteLine(value)); | ||
_queue.Add(value); | ||
|
||
return Task.FromResult(0); | ||
} | ||
} | ||
} |