简体   繁体   中英

Prism get notified when a published event has been handled

I'm publishing an event (I'm using Microsoft.Practices.Prism.Events). This event will be handled somewhere else. I then want to wait until that gets handled (I don't care where or who by) before continuing on with code, I want the handler to put some status in my event arg so I can then use that status (eg successful print or non successfully print) before deciding what to do next.

Should I just kick off a thread and check for arg.Status (which I can get the subscribers to set when handling)?

Or Alternatively should the subscriber raise another event saying publish complete or something?

public void Execute(object parameter)
{
    var arg = new PrintCustomerAccountSummaryReportRequestedEventArgument  { Customer = _viewModel.Customer, StartDate = _viewModel.ReportStartDate, EndDate = _viewModel.ReportEndDate };
    EventManager.Instance.GetEvent<PrintCustomerAccountSummaryReportRequestedEvent>().Publish(arg);
    // Wait until something has handled the event
    // then continue on and execute code.
}

The alternatives that you mentioned would be both correct. However, I would choose the second PrintResult event approach taking into account the simplicity for the solution and its extensibility if Print action would be intended from more than one site.

After handling the PrintCustomerAccountSummaryReportRequestedEvent event, the Print Subscriber would finally raise a PrintResult event where the Print Publisher would finish his job with the proper print summary report received by parameter:

public Publisher(...)
{
   ...
   PrintResultEvent printResultEvent = this.eventAggregator.GetEvent<PrintResultEvent>();
   printResultEvent.Subscribe(PrintResultEventHandler);
}

public void Execute(object parameter)
{
    var arg = new PrintCustomerAccountSummaryReportRequestedEventArgument  { Customer = _viewModel.Customer, StartDate = _viewModel.ReportStartDate, EndDate = _viewModel.ReportEndDate };
    EventManager.Instance.GetEvent<PrintCustomerAccountSummaryReportRequestedEvent>().Publish(arg);
    // Wait until something has handled the event
    // then continue on and execute code. Result will be handled in following EventHandler.
}

private void PrintResultEventHandler(PrintCustomerAccountSummaryReportRequestedEventArgument result)
{
   // Get print result and finish job accordingly.
}

I hope this helped you,

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM