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

时间:2011-11-10 11:07:07

标签: wpf wcf

我有客户端连接到双工wcf服务24/6它每周日重新启动。 在客户端iam使用listview显示一些信息。 listview itemssource绑定到自定义ObservableCollection。 客户端每分钟都会向wcf服务调用一个keepalive方法。

我的问题是客户端有活动时客户端正常工作。但是当没有活动时,应用程序只运行keepalive方法大约10-16小时。我试图添加和删除列表视图中的数据似乎没有任何作用。但是记录方法添加和删除的wcf服务工作正常。它就像用户界面没有更新。当我重新启动应用程序时,一切正常。

我该如何解决这个问题?

我的自定义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


}

以下是我将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;
        }

删除并添加方法

        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);
        }
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我所做的是实施Heartbeat机制。一切顺利。