From 6879c75bc85730e8c1c7a204fa727d876307fff9 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 2 Sep 2024 14:20:09 +0800 Subject: [PATCH] Using Queues to Improve Message Display --- v2rayN/v2rayN.Desktop/Views/MsgView.axaml | 4 +--- v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs | 23 ++++++++++---------- v2rayN/v2rayN/Views/MsgView.xaml.cs | 16 ++++++++++---- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/v2rayN/v2rayN.Desktop/Views/MsgView.axaml b/v2rayN/v2rayN.Desktop/Views/MsgView.axaml index db33c7e76b..7d50fc3bef 100644 --- a/v2rayN/v2rayN.Desktop/Views/MsgView.axaml +++ b/v2rayN/v2rayN.Desktop/Views/MsgView.axaml @@ -66,14 +66,12 @@ + IsChecked="True" /> _queueMsg = new(); + private int _numMaxMsg = 500; private string lastMsgFilter = string.Empty; private bool lastMsgFilterNotAvailable; - private ConcurrentBag _lstMsg = []; public MsgView() { @@ -74,30 +75,30 @@ public void AppendText(string msg) if (togScrollToEnd.IsChecked ?? true) { + txtMsg.CaretIndex = int.MaxValue; } } private void ShowMsg(string msg) { - if (_lstMsg.Count > 999) + if (_queueMsg.Count > _numMaxMsg) { - ClearMsg(); + for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++) + { + _queueMsg.TryDequeue(out _); + } } + _queueMsg.Enqueue(msg); if (!msg.EndsWith(Environment.NewLine)) { - _lstMsg.Add(Environment.NewLine); + _queueMsg.Enqueue(Environment.NewLine); } - _lstMsg.Add(msg); - // if (!msg.EndsWith(Environment.NewLine)) - // { - // _lstMsg.Add(Environment.NewLine); - // } - this.txtMsg.Text = string.Join("", _lstMsg); + txtMsg.Text = string.Join("", _queueMsg.ToArray()); } public void ClearMsg() { - _lstMsg.Clear(); + _queueMsg.Clear(); txtMsg.Clear(); } diff --git a/v2rayN/v2rayN/Views/MsgView.xaml.cs b/v2rayN/v2rayN/Views/MsgView.xaml.cs index 21a6973585..42bf3f4c1d 100644 --- a/v2rayN/v2rayN/Views/MsgView.xaml.cs +++ b/v2rayN/v2rayN/Views/MsgView.xaml.cs @@ -1,4 +1,5 @@ using ReactiveUI; +using System.Collections.Concurrent; using System.Reactive.Linq; using System.Text.RegularExpressions; using System.Windows.Threading; @@ -8,6 +9,8 @@ namespace v2rayN.Views public partial class MsgView { private static Config? _config; + private ConcurrentQueue _queueMsg = new(); + private int _numMaxMsg = 500; private string lastMsgFilter = string.Empty; private bool lastMsgFilterNotAvailable; @@ -80,19 +83,24 @@ public void AppendText(string msg) private void ShowMsg(string msg) { - if (txtMsg.LineCount > 999) + if (_queueMsg.Count > _numMaxMsg) { - ClearMsg(); + for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++) + { + _queueMsg.TryDequeue(out _); + } } - this.txtMsg.AppendText(msg); + _queueMsg.Enqueue(msg); if (!msg.EndsWith(Environment.NewLine)) { - this.txtMsg.AppendText(Environment.NewLine); + _queueMsg.Enqueue(Environment.NewLine); } + txtMsg.Text = string.Join("", _queueMsg.ToArray()); } public void ClearMsg() { + _queueMsg.Clear(); txtMsg.Clear(); }