From 07c3232238bae9b5ee0017a38b5b1429e7a6aaf2 Mon Sep 17 00:00:00 2001 From: aiqinxuancai Date: Tue, 3 Sep 2024 18:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SubscribeOnMainThread?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=AE=A2=E9=98=85=E6=9B=B4=E6=96=B0=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=97=B6=E5=87=BA=E7=8E=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria2Fast/MainWindow.xaml.cs | 27 ++++++++----------- Aria2Fast/Service/SubscriptionManager.cs | 8 +++--- Aria2Fast/Utils/ObservableExtensions.cs | 20 ++++++++++++++ .../View/WkySubscriptionListView.xaml.cs | 20 +++++++------- Aria2Fast/View/WkyTaskListView.xaml.cs | 20 +++++++------- 5 files changed, 53 insertions(+), 42 deletions(-) create mode 100644 Aria2Fast/Utils/ObservableExtensions.cs diff --git a/Aria2Fast/MainWindow.xaml.cs b/Aria2Fast/MainWindow.xaml.cs index efa45a7..597cd78 100644 --- a/Aria2Fast/MainWindow.xaml.cs +++ b/Aria2Fast/MainWindow.xaml.cs @@ -105,29 +105,24 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e) Aria2ApiManager.Instance.EventReceived .OfType() - .Subscribe(async r => + .SubscribeOnMainThread(async r => { - Application.Current.Dispatcher.Invoke(new Action(() => { - UpdateConnectionStatus(LinkStatus.Linking); - })); - + UpdateConnectionStatus(LinkStatus.Linking); }); Aria2ApiManager.Instance.EventReceived .OfType() - .Subscribe(async r => + .SubscribeOnMainThread(async r => { - Application.Current.Dispatcher.Invoke(new Action(() => { - if (r.IsSuccess) - { - UpdateConnectionStatus(LinkStatus.Success); - } - else - { - UpdateConnectionStatus(LinkStatus.Error); - } - })); + if (r.IsSuccess) + { + UpdateConnectionStatus(LinkStatus.Success); + } + else + { + UpdateConnectionStatus(LinkStatus.Error); + } }); diff --git a/Aria2Fast/Service/SubscriptionManager.cs b/Aria2Fast/Service/SubscriptionManager.cs index 330a705..ea6a2c2 100644 --- a/Aria2Fast/Service/SubscriptionManager.cs +++ b/Aria2Fast/Service/SubscriptionManager.cs @@ -21,6 +21,7 @@ using MemoryPack; using System.Reactive.Linq; using System.Windows; +using System.Windows.Threading; namespace Aria2Fast.Service { @@ -63,11 +64,10 @@ public SubscriptionManager() Aria2ApiManager.Instance.EventReceived .OfType() - .Subscribe(async r => + .SubscribeOnMainThread(async r => { - //成功不成功都重载 - Application.Current.Dispatcher.Invoke(() => { Restart(); }); - + Restart(); + }); } diff --git a/Aria2Fast/Utils/ObservableExtensions.cs b/Aria2Fast/Utils/ObservableExtensions.cs new file mode 100644 index 0000000..3178a87 --- /dev/null +++ b/Aria2Fast/Utils/ObservableExtensions.cs @@ -0,0 +1,20 @@ +using System; +using System.Reactive.Linq; +using System.Windows; +using System.Windows.Threading; + +public static class ObservableExtensions +{ + public static IDisposable SubscribeOnMainThread( + this IObservable source, + Action onNext) + { + return source.Subscribe(item => + { + Application.Current.Dispatcher.Invoke(() => + { + onNext(item); + }); + }); + } +} \ No newline at end of file diff --git a/Aria2Fast/View/WkySubscriptionListView.xaml.cs b/Aria2Fast/View/WkySubscriptionListView.xaml.cs index e33ac7d..e298023 100644 --- a/Aria2Fast/View/WkySubscriptionListView.xaml.cs +++ b/Aria2Fast/View/WkySubscriptionListView.xaml.cs @@ -45,18 +45,16 @@ public WkySubscriptionListView(ObservableCollection viewModel Aria2ApiManager.Instance.EventReceived .OfType() - .Subscribe(async r => + .SubscribeOnMainThread(async r => { - Application.Current.Dispatcher.Invoke(new Action(() => { - if (r.IsSuccess) - { - this.SubscriptionButton.IsEnabled = true; - } - else - { - this.SubscriptionButton.IsEnabled = false; - } - })); + if (r.IsSuccess) + { + this.SubscriptionButton.IsEnabled = true; + } + else + { + this.SubscriptionButton.IsEnabled = false; + } }); } diff --git a/Aria2Fast/View/WkyTaskListView.xaml.cs b/Aria2Fast/View/WkyTaskListView.xaml.cs index 51e1126..f6968f4 100644 --- a/Aria2Fast/View/WkyTaskListView.xaml.cs +++ b/Aria2Fast/View/WkyTaskListView.xaml.cs @@ -44,18 +44,16 @@ public WkyTaskListView(ObservableCollection viewModel) Aria2ApiManager.Instance.EventReceived .OfType() - .Subscribe(async r => + .SubscribeOnMainThread(async r => { - Application.Current.Dispatcher.Invoke(new Action(() => { - if (r.IsSuccess) - { - this.AddTaskButton.IsEnabled = true; - } - else - { - this.AddTaskButton.IsEnabled = false; - } - })); + if (r.IsSuccess) + { + this.AddTaskButton.IsEnabled = true; + } + else + { + this.AddTaskButton.IsEnabled = false; + } });