views:

103

answers:

2

I'm using an MVC pattern in winforms application. I need to call remote service asynchronously. So On some event in View I invoke corresponding Presenter method. In Presenter I call BeginInvoke method of service. But to View must be updated only in Main Thread. I could actualy point CallBack to some function in View, and update it`s controls state, but this conflicts with MVP pattern - View must not be responsible for data it carries. This callback function must be in Presenter. But how then invoke View in Main Thread?

A: 

do you assume your form by View? if yes, you can call yourForm.Invoke( put delegate here ); , this will invoke the delegate in main thread. But why do you want to execute it in main thread? why can't you execute in thread of callback?

Andrey
A: 

Put the callback function in the presenter. Have the presenter call whatever update function on the view is required/have the view observe the presenter's state and handle the 'completed' event. In the view's function, if the view is implemented by a windows Form, test the InvokeRequired property to see if the call has come in on the windows thread. If it hasn't, then use Invoke to invoke it instead.

    private void SetMessage(string message)
    {
        if (InvokeRequired)
        {
            BeginInvoke(new Action(() => SetMessage(message)));
            return;
        }

        button1.Text = message;
    }
Pete Kirkham