Carlos Blanco
Carlos Blanco

Reputation: 8762

Is there a way to assure that one method executes after another when using DispatcherObjectInvoker.Invoke

Is there a way to assure the second statement executes after the first one?

DispatcherObjectInvoker.Invoke(_layouts,
    () =>
        _layouts.RaiseEvent(new LayoutEventArgs(
            MainScreen.ChangedLayoutEvent,
            this)));

DispatcherObjectInvoker.Invoke(_layouts, Grid.Refresh);

I'm accessing an Grid that is touched in the first statement. The refresh of the grid is working in about 80% of the cases only. I obviously have a race condition here.

Upvotes: 0

Views: 77

Answers (2)

Eric Liprandi
Eric Liprandi

Reputation: 5574

Not sure what a DispatcherObjectInvoker is, but I will assume it's a wrapper for Dispatcher or something.

If it is, if you call Invoke(), it is a blocking call. So your second call is guaranteed to be after your first call.

However, if your intent is to have those be asynchronous calls, you should use BeginInvoke. The DispatcherPriority parameter helps ensure that calls made to the same priority level are in the same order. So, if _layouts refers to the DispatcherPriority.Loaded priority (or any other priority), the calls are guaranteed to be in the same order.

Check the MSDN documentation.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499810

Why not make them both part of the same action?

DispatcherObjectInvoker.Invoke(_layouts, () => 
{
     _layouts.RaiseEvent(new LayoutEventArgs(MainScreen.ChangedLayoutEvent,
                                             this));
     Grid.Refresh();
 });

Upvotes: 3

Related Questions