Skip to content

Commit

Permalink
增加SubscribeOnMainThread避免订阅更新线程时出现问题
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Sep 3, 2024
1 parent b7f4adb commit 07c3232
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
27 changes: 11 additions & 16 deletions Aria2Fast/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,24 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e)

Aria2ApiManager.Instance.EventReceived
.OfType<LoginStartEvent>()
.Subscribe(async r =>
.SubscribeOnMainThread(async r =>
{
Application.Current.Dispatcher.Invoke(new Action(() => {
UpdateConnectionStatus(LinkStatus.Linking);
}));

UpdateConnectionStatus(LinkStatus.Linking);
});


Aria2ApiManager.Instance.EventReceived
.OfType<LoginResultEvent>()
.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);
}

});

Expand Down
8 changes: 4 additions & 4 deletions Aria2Fast/Service/SubscriptionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using MemoryPack;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Threading;

namespace Aria2Fast.Service
{
Expand Down Expand Up @@ -63,11 +64,10 @@ public SubscriptionManager()

Aria2ApiManager.Instance.EventReceived
.OfType<LoginResultEvent>()
.Subscribe(async r =>
.SubscribeOnMainThread(async r =>
{
//成功不成功都重载
Application.Current.Dispatcher.Invoke(() => { Restart(); });

Restart();

});
}

Expand Down
20 changes: 20 additions & 0 deletions Aria2Fast/Utils/ObservableExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(
this IObservable<T> source,
Action<T> onNext)
{
return source.Subscribe(item =>
{
Application.Current.Dispatcher.Invoke(() =>
{
onNext(item);
});
});
}
}
20 changes: 9 additions & 11 deletions Aria2Fast/View/WkySubscriptionListView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,16 @@ public WkySubscriptionListView(ObservableCollection<SubscriptionModel> viewModel

Aria2ApiManager.Instance.EventReceived
.OfType<LoginResultEvent>()
.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;
}

});
}
Expand Down
20 changes: 9 additions & 11 deletions Aria2Fast/View/WkyTaskListView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,16 @@ public WkyTaskListView(ObservableCollection<TaskModel> viewModel)

Aria2ApiManager.Instance.EventReceived
.OfType<LoginResultEvent>()
.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;
}

});

Expand Down

0 comments on commit 07c3232

Please sign in to comment.