开发者

updating bindingList on the ui thread using invoke multiple times

I have an application, where I need to update the view based on the messages received from a wcf service. I am using the MVP for the ui. The view has a dataGridView on a win form, in which is it shows the messages received. I have a binding list for this view in my presenter.

I update the binding list by calling invoke on the dataGridView. My question is if I am receiving a lot of messages (~ 10/sec) does it make sense to gather all those messages in a separate list and then inside the invoke call, add them to the binding list or call invoke for adding each message to the binding list.

public class Presenter
{
    private List<ClientMessage> newMessages;
    private BindingList<ClientMessage> messageDataSource;
    public void Presenter()
    {
        newMessages = new List<ClientMessage>();
        Views.AuditorGridView.DataSource = messageDataSource;
    }

    public void ReceiveMessages(List<ClientMessage> messageList)
    {
        //...some other message related processing
        foreach (ClientMessage message in messageList)
        {
            if (messageIdList.Contains(message.ID)) continue;
            messageIdList.Add(message.ID);
       开发者_Python百科     messageDataSource.Add(message);
            View.gridView.BeginInvoke(new InvokeDelegate(GridInvokeMethod1));
        }
    }

    private void GridInvokeMethod1()
    {
        Views.AuditorGridView.DataSource = messageDataSource;
    }

    //OR

    public void ReceiveMessages(List<ClientMessage> messageList)
    {
        //...some other message related processing
        newMessages.Clear();
        foreach (ClientMessage message in messageList)
        {
            if (messageIdList.Contains(message.ID)) continue;
            messageIdList.Add(message.ID);
            newMessages.Add(message);
        }
        View.gridView.BeginInvoke(new InvokeDelegate(GridInvokeMethod2));
    }

    private void GridInvokeMethod2()
    {
        foreach (ClientMessage message in newMessages)
        {
            messageDataSource.Add(message);
        }
    }
}


Have you tried using BindingSource. you can suspend your bindings (so that you can add new items in your list and then resume it). That way your Grid will get bulk updates.

You might have to use BindingList for that though (so that updates on the Source gets reflected in the Target (which is Grid in your case)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜