WPF事件聚合器-等待先前的执行完成

问题描述 投票:0回答:1

我正在使用Prism开发WPF应用程序。我正在使用EventAggregator在视图模型之间进行通信。

public class PublisherViewModel
{
   _eventAggregator.GetEvent<RefreshEvent>().Publish("STOCKS");
}
public class SubscriberViewModel
{
    public SubscriberViewModel(IEventAggregator ea)
    {
        ea.GetEvent<RefreshEvent>().Subscribe(RefreshData);
    }

    void RefreshData(string category)
    {
        Task.Run(() =>
        {
                //Long Running Operation

                Dispatcher.Invoke(() =>
                {
                     //Refresh UI
                });
        });
    }
}

PublisherViewModel可以一个接一个地发布事件。但是,在SubscriberViewModel上,因为我运行了很长时间的[[Task,并且没有等待(我无法),所以来自发布者的第二个请求立即开始执行。在SubscriberViewModel,我要处理所有传入的请求,以使它们按到达的顺序依次执行。

我正在考虑使用基于队列的机制来处理此问题。

您能建议我同样的最佳做法吗?

谢谢!

c# wpf prism eventaggregator
1个回答
0
投票
我正在考虑使用基于队列的机制来处理此问题。

这是要走的路。设置一个队列(可能是异步队列),将事件推送到订阅服务器中,并从辅助任务中使用它们。

TPL Dataflow是执行此操作的一个选项:从处理程序中创建一个ActionBlock<string>,并在事件进入时将事件发布到它。

© www.soinside.com 2019 - 2024. All rights reserved.