从Silverlight排队WCF调用

时间:2013-02-17 11:28:15

标签: c# silverlight asynchronous queue

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

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

_wcfserviceagent.GetCustomer(customer, callback);
}

  void callback(ObservableCollection<CustomerType> customer)
{

//do some action

}

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

无论如何都要确保始终按顺序调用呼叫和回叫?

理想情况下,我希望以这样的方式执行:对于一个事件,它将调用服务和回调,并且其间的任何其他调用将排队。当然,我无法阻止UI线程。

1 个答案:

答案 0 :(得分: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());
    }
}
相关问题