using Bro.Common.Helper; using Bro.UI.Model.Winform; using System; using System.Collections.Concurrent; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Bro.UI.Config.MenuForms { [DockOption(WeifenLuo.WinFormsUI.Docking.DockState.DockBottom, 0, 200)] [MenuNode("Log", "日志", 3, "View1", true)] public partial class FrmLog : MenuFrmBase { public FrmLog() { InitializeComponent(); } readonly ConcurrentQueue _logQueue = new ConcurrentQueue(); Task _logTask = null; static readonly object _logLock = new object(); public override void LogDisplay(LogMsg msg) { _logQueue.Enqueue(msg); lock (_logLock) { if (_logTask == null) { _logTask = Task.Run(() => { while (true) { StringBuilder txt = new StringBuilder(); while (_logQueue.Count > 0) { if (_logQueue.TryDequeue(out LogMsg log)) { txt.AppendLine($"{log.LogTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t{log.Prefix}\t{log.Msg}"); } } Invoke(new Action(() => { //if (txtLog.IsHandleCreated) { txtLog.AppendText(txt.ToString()); } })); Thread.Sleep(1000); } }); } } } private void tsmiClearLog_Click(object sender, EventArgs e) { txtLog.Clear(); } } }