Skip to content

Commit

Permalink
Fix cross-thread issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 23, 2023
1 parent eb0fecf commit c43ef31
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Leo.UI;
using Leo.Wpf.App.Messages;
using Microsoft.Extensions.Logging;
using static Leo.Wpf.App.ViewModels.CustomerViewModel;

namespace Leo.Wpf.App.ViewModels
Expand All @@ -24,6 +25,7 @@ public sealed partial class MainWindowViewModel : ObservableRecipient, IDisposab
private readonly INewCustomerDetailWindowService _newCustomerDetailWindow;
private readonly IFindWindowService _findWindow;
private readonly IEchoWindowService _echoWindow;
private readonly ILogger<MainWindowViewModel> _logger;

public MainWindowViewModel(
ICustomerService customerService,
Expand All @@ -34,7 +36,8 @@ public MainWindowViewModel(
INewCustomerDetailWindowService newCustomerDetailWindowService,
IFindWindowService findWindowService,
IEchoWindowService echoWindowService,
IMessenger messenger) : base(messenger)
IMessenger messenger,
ILogger<MainWindowViewModel> logger) : base(messenger)
{
_customerService = customerService;
_detailService = detailService;
Expand All @@ -44,20 +47,30 @@ public MainWindowViewModel(
_newCustomerDetailWindow = newCustomerDetailWindowService;
_findWindow = findWindowService;
_echoWindow = echoWindowService;
_logger = logger;

Messenger.Register<CustomerCreatedMessage>(this, (rcpt, msg) =>
{
Task.Run(() => ReloadCurrentCustomerAsync(msg.Id));
_ = ReloadCurrentCustomerAsync(msg.Id);
});

Messenger.Register<CustomerFoundMessage>(this, (rcpt, msg) =>
{
Task.Run(() => ReloadCurrentCustomerAsync(msg.Id));
ReloadCurrentCustomerAsync(msg.Id).ContinueWith(t =>
{
if (t.Exception is not null)
{
_logger.LogError(t.Exception, t.Exception.Message);
}
});
});

Messenger.Register<CustomerDetailCreatedMessage>(this, (rcpt, msg) =>
{
Task.Run(() => ReloadCurrentCustomerAsync(msg.customerId));
Task.Factory.StartNew(() => ReloadCurrentCustomerAsync(msg.customerId),
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
});
}

Expand Down

0 comments on commit c43ef31

Please sign in to comment.