Question

I am work my way through different open source projects to get a feel for how people approach the same problem in different ways. This time it's Event Aggregators, specifically Caliburn.Micro's one.

I notice that Rob said that if no thread is supplied that the EA will marshal to the UI thread. The line specifically is:

public static Action<System.Action> 
DefaultPublicationThreadMarshaller = action => action();

and then further on:

marshal(() =>
         {
           *SNIP*
         });

What I don't understand his how can you say this will be called on the UI thread, are all actions called on the UI thread unless you specify one that is not? (You can specify which action is called btw, the line above is just if no action is supplied).

Was it helpful?

Solution

Action can be invoked either in the thread that calls it:

Action a = () => Foo();
a.Invoke();

Or it can be invoked asynchronously

Action a = () => Foo();
a.BeginInvoke();

If the event aggregator is configured to use a synchronous call then it is possible that the event gets handled in the UI thread. However it is very unlikely, and usually an event aggregator uses asynchronous execution and the action is executed in a thread pool.

In the code in question there will be a place, where it (internally and not included here) calls either Invoke or BeginInvoke on the action, this is where the difference is.

N.B. This answer is not specific to any particular event aggregator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top