Wait for an event to finish (threading)


I need to wait for an event to finish before continuing.

Here is my code:

foreach (KeyValuePair<string, Stream> pair in this.XMLCollection) { ... this.eventAggregator.GetEvent<LogToApplicationEvent>().Publish(credentials); //wait ... }

Before continuing I need to wait for "login" event to execute complately. I tried using Task.Factory, but it did not work for me, or I cant use it right...

This code is on presenter, but the event updates the main UI.

//publish public virtual void Publish(TPayload payload) { base.InternalPublish(payload); }


At least two possible solutions:


Use a <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow">BackgroundWorker</a> to execute your code, and use the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx" rel="nofollow">RunWorkerCompleted</a> event to execute the code that is run after completion.

A BackgroundWorker wraps the event based asynchronous pattern into a very easy to use mechanism, complete with progress reporting and cancellation. See this <a href="http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx" rel="nofollow">BackgroundWorker tutorial</a> and this SO answer .

<strong>Tasks (.NET 4.0 and above)</strong>

Use a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" rel="nofollow">Task</a> object, and use the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.continuewith.aspx" rel="nofollow">ContinueWith</a> method to define the code that needs to be executed after completion of the first task.


Event Aggregator publishing and subscribing event pattern is synchronous. You need not to worry about it.

So, it won't resume until its subscribers are finished executing its delegates.

<strong>Assumption</strong> - You are using inbuilt <a href="http://msdn.microsoft.com/en-us/library/ff921122.aspx" rel="nofollow">Event Aggregator</a> class provided by Microsoft.


  • QPushButton alignment on top another widget
  • Windows Forms Form hanging after calling show from another thread
  • Error handling in WPF PasswordBox
  • How to Moq this view?
  • Error building image: Error building rlang - not available on R 3.5.0 (Uploading shiny)
  • How to exclude files in a custom clean task?
  • Install different versions of nuget packages inside one solution file with two projects
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Rails 3, Custom Actions, and HTML request methods
  • Picasso Taking time to load images
  • get all processes in parallel
  • goJS dropdown remove items
  • How to override __call__ in celery on main?
  • Get the last date of each month in a list of dates in Python
  • Manually Timing out a C# Thread
  • Main Thread Checker: UI API called on a background thread iOS 11 Xcode 9 GM Seed
  • Jenkins Grails plugin does not list lastest versions of Grails
  • Activation Function choice for Neural network
  • How can I replace the server in Web Component Tester
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • SplFileObject error failed to open stream: No such file or directory
  • Change JButton Shape while respecting Look And Feel
  • Projection media query: browser support and workarounds?
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • output of program is not same as passed argument
  • How can I use Kendo UI with Razor?
  • jQuery tmpl and DataLink beta
  • Delete MySQLi record without showing the id in the URL
  • Akka Routing: Reply's send to router ends up as dead letters
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Unable to use reactive element in my shiny app
  • How to load view controller without button in storyboard?