【问题标题】:Queueing WCF calls from silverlight从 silverlight 排队 WCF 调用
【发布时间】:2013-02-17 11:28:14
【问题描述】:

在我的 Silverlight 用户控件中,我正在监听来自应用程序的事件并调用 WCF 服务来执行一些操作

void SelectedCustomerEvent(string customer)
{
//.......

_wcfserviceagent.GetCustomer(customer, callback);
}

  void callback(ObservableCollection<CustomerType> customer)
{

//do some action

}

在某些情况下,执行某些操作时会多次触发事件。问题是回调不一定按照调用 WCF 服务的顺序调用。

有没有办法确保调用和回调总是按顺序调用?

理想情况下,我希望以这样一种方式执行,即对于事件,它将调用服务和回调,而介于两者之间的任何其他调用都将排队。当然,我不能阻塞 UI 线程。

【问题讨论】:

    标签: c# silverlight asynchronous queue


    【解决方案1】:

    确保对 WCF 服务的调用顺序的唯一方法是在客户端上实现您自己的队列。

    例如:

    Queue<string> _customersQueue = new Queue<string>();
    bool _fetching;
    void SelectedCustomerEvent(string customer)
    {
        _customersQueue.Enqueue(customer);
        //.......
        if (!_fetching)
        {
            DoFetchCustomer(_customersQueue.Dequeue());
        }
    }
    
    void DoFetchCustomer(string customer)
    {
        _fetching = true;
        _wcfserviceagent.GetCustomer(customer, callback);
    }
    
    void callback(ObservableCollection<CustomerType> customer)
    {
        _fetching = false;
        //do some action
        if (_customersQueue.Count > 0)
        {
            DoFetchCustomer(_customersQueue.Dequeue());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多