Skip to content

Commit

Permalink
optimize GameLog form
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Jan 13, 2022
1 parent 8ab9c1b commit 56e71bd
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions CmlLibWinFormSample/GameLog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace CmlLibWinFormSample
Expand All @@ -10,30 +11,34 @@ public partial class GameLog : Form
public GameLog(Process process)
{
InitializeComponent();

process.ErrorDataReceived += Process_DataReceived;
process.OutputDataReceived += Process_DataReceived;
output(process.StartInfo.Arguments);
}

private readonly ConcurrentQueue<string> logQueue = new ConcurrentQueue<string>();

private void Process_DataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
logQueue.Enqueue(e.Data);
}

private void output(string msg) => logQueue.Enqueue(msg);

private void timer1_Tick(object sender, EventArgs e)
{
string msg;
while (logQueue.TryDequeue(out msg))
if (logQueue.Count == 0)
return;

var sb = new StringBuilder();
while (logQueue.TryDequeue(out string msg))
{
richTextBox1.AppendText(msg + "\n");
richTextBox1.ScrollToCaret();
sb.AppendLine(msg);
}
richTextBox1.AppendText(sb.ToString());
richTextBox1.ScrollToCaret();
}
}
}

0 comments on commit 56e71bd

Please sign in to comment.