简体   繁体   English

WCF双工客户端空闲不会更新用户界面

[英]WCF Duplex client idle wont update userinterface

I have clients thats is connected to a duplex wcf service 24/6 it is restarted every sunday. 我的客户端多数民众赞成在连接到双工wcf服务24/6,它在每个星期日重新启动。 In the client iam using a listview to display some information. 在客户端iam中使用listview显示一些信息。 The listview itemssource is binded to a custom ObservableCollection. listview项目源绑定到自定义的ObservableCollection。 The client is calling a keepalive method every minute to the wcf service. 客户端每分钟都会对wcf服务调用一个keepalive方法。

My problem here is the client works fine when there is activity in the client. 我的问题是,当客户端中有活动时,客户端工作正常。 But when there is no activity and the application just run the keepalive method for about 10-16 hours. 但是,当没有活动并且应用程序仅运行keepalive方法大约10-16小时时。 And iam trying to add and remove data to the listview it seems that nothing works. 而且IAM试图向列表视图添加和删除数据,似乎没有任何效果。 But the wcf service logging the method add and remove is working fine. 但是记录方法添加和删除的wcf服务运行良好。 Its like the userinterface isnt updating. 就像用户界面没有更新一样。 When i restart the application everything works fine. 当我重新启动应用程序时,一切正常。

how do i solve this problem ? 我该如何解决这个问题?

My custom ObservableCollection object code : 我的自定义ObservableCollection对象代码:

    public class ObservableOrderResponseQueue : INotifyCollectionChanged, IEnumerable<OrderResponse>
{
    public event NotifyCollectionChangedEventHandler CollectionChanged = (o, e) => { };
    private List<OrderResponse> _list = new List<OrderResponse>();

    /// <summary>
    /// Adds to the list.
    /// </summary>
    /// <param name="orderResponse">OrderResponse.</param>
    public void Add(OrderResponse orderResponse)
    {
        //Only 6 items in list is possible if more then 6 remove the first item.
        if (_list.Count >= 6)
        {
            RemoveAt(0);
        }

        this._list.Add(orderResponse);            

        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, orderResponse, (_list.Count - 1)));
    }

    /// <summary>
    /// Remove from list.
    /// </summary>
    /// <param name="index">Item index to remove.</param>
    public void RemoveAt(int index)
    {
        OrderResponse order = this._list[index];
        this._list.RemoveAt(index);

        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, order, index));
    }

    /// <summary>
    /// Remove from list.
    /// </summary>
    /// <param name="orderResponse">Item to be removed.</param>
    public void Remove(OrderResponse orderResponse)
    {
        if (_list.Count == 0) return;

        var item = _list.Where(o => o.OrderDetail.TrayCode == orderResponse.OrderDetail.TrayCode).FirstOrDefault();
        int index = _list.IndexOf(item);

        if (index == -1) return;

        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));

        this._list.RemoveAt(index);
    }

    #region IEnumerable<OrderResponse> Members

    public IEnumerator<OrderResponse> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    #endregion


}

Here is how i bind the usercontrol to the class. 这是我将usercontrol绑定到类的方式。

            //Set up client callbacks events
        App.clientBack.ClientNotified += new ClientNotifiedEventHandler(clientBack_ClientNotified);
        App.clientBack.AddToDisplayEvent += new AddToDisplayEventHandler(clientBack_AddToDisplayEvent);
        App.clientBack.RemoveFromDisplayEvent += new RemoveFromDisplayEventHandler(clientBack_RemoveFromDisplayEvent);
        App.clientBack.UpdateQueueDisplayEvent += new UpdateQueueDisplayEventHandler(clientBack_UpdateQueueDisplayEvent);


        //Show one chair or many.
        if (_settings.IsOneChair)
        {
            userControlOneChair.ItemSource = _queueProductionItems;
        }
        else
        {
            userControlChairs.ItemsSource = _queueProductionItems;
        }

Remove and add methods 删除和添加方法

        void clientBack_RemoveFromDisplayEvent(object sender, RemoveFromDisplayEventArgs e)
    {
        try
        {
            _logger.Info("Remove from display.");

            userControlChairs.Dispatcher.Invoke((Action)(() =>
            {
                _queueProductionItems.Remove(e.OrderResponse);
            }));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    void clientBack_AddToDisplayEvent(object sender, AddToDisplayEventArgs e)
    {
        try
        {
            _logger.Info("Add to display.");

            userControlChairs.Dispatcher.Invoke((Action)(() =>
            {
                _queueProductionItems.Add(e.OrderResponse);
            }));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Thanks for help! 感谢帮助!

What i did was implementing a Heartbeat mechanism. 我所做的是实现心跳机制。 And it all worked out. 一切都解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM