Skip to content

Commit

Permalink
Merge pull request #389 from OmniSharp/troy/sharedtestwriter
Browse files Browse the repository at this point in the history
Update SharedTextWriter
  • Loading branch information
david-driscoll committed Feb 2, 2016
2 parents 06eabc8 + c2a54cf commit 98bba51
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/OmniSharp.Stdio/Services/SharedTextWriter.cs
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);
}
}
}

0 comments on commit 98bba51

Please sign in to comment.