Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call async methods from within a Prism event aggregator subscriber?

Quick question - how do I call async methods from within a Prism event aggregator subscriber action? E.g.

_eventAggregator.GetEvent<PubSubEvent<SomeMessage>>()
   .Subscribe((msg) => {
                       DoSomething();
                       await DoSomethingAsync();
                       DoSomethingElse();
                    });
like image 907
Andrew Stephens Avatar asked Jun 09 '16 12:06

Andrew Stephens


2 Answers

Just making @Charles Mager a answer

_eventAggregator.GetEvent<PubSubEvent<SomeMessage>>()
   .Subscribe(async(msg) => {
                       DoSomething();
                       await DoSomethingAsync();
                       DoSomethingElse();
                    });
like image 163
LeRoy Avatar answered Sep 20 '22 10:09

LeRoy


With current Prism implementation, it is not possible see here.

But I have found an alternate implementation of Prism's EventAggregator, which allows async subscriptions. See here.

This new event async aggregator overloads can be used in the same way as the original one:

Subscription to event is exactly the same with another overload:


    _eventAggregator.GetEvent<<TfsHookEvent<WorkItemUpdatedPayload>>().Subscribe(WorkItem_Updated);

Event handler:


    private async Task WorkItem_Updated(WorkItemUpdatedPayload obj)
    {
        await CheckAsync();
    }
like image 27
Karel Kral Avatar answered Sep 21 '22 10:09

Karel Kral