Skip to content

Commit d08c0c2

Browse files
Cleaner way of handling console buffer.
1 parent 7d8ea53 commit d08c0c2

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

MinerControl/MainWindow.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,16 @@ private string ActiveTime(PriceEntryBase priceEntry)
240240
return time.FormatTime();
241241
}
242242

243+
SlidingBuffer<string> _consoleBuffer = new SlidingBuffer<string>(200);
244+
243245
private void WriteConsole(string text)
244246
{
245247
Invoke(new MethodInvoker(
246248
delegate
247249
{
248-
textConsole.AppendText(text + Environment.NewLine);
249-
int numOfLines = textConsole.Lines.Length - 200;
250-
if (numOfLines <= 0) return;
251-
252-
var lines = textConsole.Lines;
253-
var newLines = lines.Skip(numOfLines);
250+
_consoleBuffer.Add(text);
254251

255-
textConsole.Lines = newLines.ToArray();
252+
textConsole.Lines = _consoleBuffer.ToArray();
256253
textConsole.Focus();
257254
textConsole.SelectionStart = textConsole.Text.Length;
258255
textConsole.SelectionLength = 0;

MinerControl/MinerControl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Services\WestHashService.cs" />
8585
<Compile Include="PriceEntries\YaampPriceEntry.cs" />
8686
<Compile Include="Services\YaampService.cs" />
87+
<Compile Include="Utility\SlidingBuffer.cs" />
8788
<Compile Include="Utility\SortableBindingList.cs" />
8889
<Compile Include="Utility\PropertyChangedBase.cs" />
8990
<Compile Include="Utility\WebUtil.cs" />

MinerControl/Utility/SlidingBuffer.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
4+
namespace MinerControl.Utility
5+
{
6+
// Taken from http://stackoverflow.com/questions/6392516
7+
8+
public class SlidingBuffer<T> : IEnumerable<T>
9+
{
10+
private readonly Queue<T> _queue;
11+
private readonly int _maxCount;
12+
13+
public SlidingBuffer(int maxCount)
14+
{
15+
_maxCount = maxCount;
16+
_queue = new Queue<T>(maxCount);
17+
}
18+
19+
public void Add(T item)
20+
{
21+
if (_queue.Count == _maxCount)
22+
_queue.Dequeue();
23+
_queue.Enqueue(item);
24+
}
25+
26+
public IEnumerator<T> GetEnumerator()
27+
{
28+
return _queue.GetEnumerator();
29+
}
30+
31+
IEnumerator IEnumerable.GetEnumerator()
32+
{
33+
return GetEnumerator();
34+
}
35+
36+
public int Count
37+
{
38+
get { return _queue.Count; }
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)