Question

When dealing with multithreading normally Presenter receives event and calls View to update the corresponding control.

Presenter:

private void EventAggregator_InfoUpdated(object sender, InfoUpdatedEventArgs e)
{
    view.UpdateFeedInfo(e.FeedInfo);
}

View:

public void UpdateFeedInfo(FeedInfo feedInfo)
{
    if (!control.IsHandleCreated && !control.IsDisposed) return;

    control.BeginInvoke((MethodInvoker) (() => control.Update(feedInfo)));
}

My question is how to invoke a method call in presenter in the GUI thread before calling view. Something like:

private void EventAggregator_InfoUpdated(object sender, InfoUpdatedEventArgs e)
{
    //InvokeInUiThread// ManageInfoInput(e.FeedInfo);
}

private void ManageInfoInput(FeedInfo feedInfo)
{
    ...
    view.UpdateFeedInfo(e.FeedInfo);
}
Was it helpful?

Solution

I'll modify Wiktor idea for using View to invoke presenter code in UI thread.

View interface:

public interface IView
{
    IAsyncResult BeginInvoke(Delegate method);
    object Invoke(Delegate method);
}

Presenter:

private void EventAggregator_InfoUpdated(object sender, InfoUpdatedEventArgs e)
{
    view.Invoke(new Action(() => ManageInfoInput(e.FeedInfo)));
}

private void ManageInfoInput(FeedInfo feedInfo)
{
    ...
    view.UpdateFeedInfo(feedInfo);
}

In that way we don't put any code in View.

OTHER TIPS

View:

public void ExecuteDelegateOnUIThread( Delegate action )
{
   this.Invoke( action ); 
}

Presenter:

view.ExecuteDelegateOnUIThread( () => { arbitrary code } );

I don't think there is another way, you somehow just have to refer to the UI thread the view is created on and the easiest way is just to ask the view to schedule the execution on a proper thread with Invoke.

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