From 4f154acfad6926c0d9bb6ddf7cdb7803fb2af697 Mon Sep 17 00:00:00 2001 From: nanikit Date: Wed, 4 Mar 2020 07:03:37 +0900 Subject: [PATCH] fix: log update bottleneck --- eztrans-server/MainWindow.xaml | 8 ++++- eztrans-server/MainWindow.xaml.cs | 51 ++++++++++++++++++------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/eztrans-server/MainWindow.xaml b/eztrans-server/MainWindow.xaml index a7eaf24..3ede92a 100644 --- a/eztrans-server/MainWindow.xaml +++ b/eztrans-server/MainWindow.xaml @@ -11,7 +11,13 @@ - + + + + + diff --git a/eztrans-server/MainWindow.xaml.cs b/eztrans-server/MainWindow.xaml.cs index 02d1a09..c515e52 100644 --- a/eztrans-server/MainWindow.xaml.cs +++ b/eztrans-server/MainWindow.xaml.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Threading; namespace eztrans_server { /// @@ -15,13 +17,29 @@ namespace eztrans_server { public partial class MainWindow : Window { public MainWindow() { var vm = new HttpServerVM(); + DataContext = vm; + InitializeComponent(); + PrintProgramVersion(vm); + vm.Logs.CollectionChanged += LogVmItem; _ = vm.Restart(); + } - DataContext = vm; - InitializeComponent(); + private void LogVmItem(object sender, NotifyCollectionChangedEventArgs e) { + if (e.Action == NotifyCollectionChangedAction.Add) { + Log(e.NewItems[0] as string ?? ""); + } } + private void Log(string s) => Dispatcher.InvokeAsync(() => { + BlockCollection blocks = TbLog.Document.Blocks; + if (blocks.Count >= 1000) { + blocks.Remove(blocks.FirstBlock); + } + var paragraph = new Paragraph(new Run(s)); + blocks.Add(paragraph); + }); + private void LogScrollDown(object sender, TextChangedEventArgs e) { double bottom = TbLog.VerticalOffset + TbLog.ViewportHeight; bool isBottommost = bottom >= TbLog.ExtentHeight - 10; @@ -30,10 +48,11 @@ private void LogScrollDown(object sender, TextChangedEventArgs e) { } } - private static void PrintProgramVersion(HttpServerVM vm) { + private void PrintProgramVersion(HttpServerVM vm) { string build = Properties.Resources.BuildDate; string date = $"{build.Substring(2, 2)}{build.Substring(5, 2)}{build.Substring(8, 2)}"; - vm.Logs.Add($"eztrans-server v{date} by nanikit"); + TbLog.Document.Blocks.Clear(); + Log($"eztrans-server v{date} by nanikit"); } } @@ -42,12 +61,6 @@ class HttpServerVM : ViewModelBase { public RelayCommand RestartCommand { get; private set; } - private string _Log = ""; - public string Log { - get => _Log; - set => Set(ref _Log, value); - } - private string _Origin = "http://localhost:8000/"; public string Origin { get => _Origin; @@ -66,8 +79,6 @@ public string Title { public HttpServerVM() { RestartCommand = new RelayCommand(() => _ = Restart()); Logs = new ObservableCollection(); - Logs.CollectionChanged += MergeLogs; - SetOriginFromCommandLine(); } @@ -143,15 +154,13 @@ private void OnRequest(IPEndPoint ip, string? req) { string head = req?.Substring(0, Math.Min(40, req.Length)) ?? ""; string log = $"{datetime} {ip.Address}: {head}"; - if (Logs.Count > 1000) { - Logs.RemoveAt(0); - } - Logs.Add(log); - Title = $"요청 수: {++RequestCount}"; - } - - private void MergeLogs(object sender, NotifyCollectionChangedEventArgs e) { - Log = string.Join("\n", Logs); + Application.Current.Dispatcher.InvokeAsync(() => { + if (Logs.Count > 1000) { + Logs.RemoveAt(0); + } + Logs.Add(log); + Title = $"요청 수: {++RequestCount}"; + }); } } }