From 8fc9cc18a3e70b697ae1113e980e5ea595ec75e6 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 28 Mar 2021 00:21:48 -0700 Subject: [PATCH] Simplify dispatcher calls --- MPF/UserControls/LogOutput.xaml.cs | 48 +++++++----------------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/MPF/UserControls/LogOutput.xaml.cs b/MPF/UserControls/LogOutput.xaml.cs index 8f2fce4d..e95b7b27 100644 --- a/MPF/UserControls/LogOutput.xaml.cs +++ b/MPF/UserControls/LogOutput.xaml.cs @@ -282,19 +282,11 @@ namespace MPF.UserControls /// Color to print text in private void AppendToTextBox(string text, Brush color) { - if (Application.Current.Dispatcher.CheckAccess()) + Dispatcher.Invoke(() => { - Run run = new Run(text) { Foreground = color }; + var run = new Run(text) { Foreground = color }; _paragraph.Inlines.Add(run); - } - else - { - Dispatcher.Invoke(() => - { - Run run = new Run(text) { Foreground = color }; - _paragraph.Inlines.Add(run); - }); - } + }); } /// @@ -303,25 +295,14 @@ namespace MPF.UserControls /// Text to append private string GetLastLine() { - if (Application.Current.Dispatcher.CheckAccess()) + return Dispatcher.Invoke(() => { if (!_paragraph.Inlines.Any()) return null; var last = _paragraph.Inlines.LastInline as Run; return last.Text; - } - else - { - return Dispatcher.Invoke(() => - { - if (!_paragraph.Inlines.Any()) - return null; - - var last = _paragraph.Inlines.LastInline as Run; - return last.Text; - }); - } + }); } /// @@ -416,10 +397,10 @@ namespace MPF.UserControls /// Text to check and update with private void ProcessStringForProgressBar(string text, Matcher? matcher) { - if (Application.Current.Dispatcher.CheckAccess()) + Dispatcher.Invoke(() => + { matcher?.Apply(ref text); - else - Dispatcher.Invoke(() => { matcher?.Apply(ref text); }); + }); } /// @@ -429,21 +410,12 @@ namespace MPF.UserControls /// Color to print text in private void ReplaceLastLine(string text, Brush color) { - if (Application.Current.Dispatcher.CheckAccess()) + Dispatcher.Invoke(() => { var last = _paragraph.Inlines.LastInline as Run; last.Text = text; last.Foreground = color; - } - else - { - Dispatcher.Invoke(() => - { - var last = _paragraph.Inlines.LastInline as Run; - last.Text = text; - last.Foreground = color; - }); - } + }); } #endregion