c# - 排队来自 silverlight 的 WCF 调用

标签 c# silverlight asynchronous queue

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

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

_wcfserviceagent.GetCustomer(customer, callback);
}

  void callback(ObservableCollection<CustomerType> customer)
{

//do some action

}

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

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

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

最佳答案

确保 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());
    }
}

关于c# - 排队来自 silverlight 的 WCF 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920575/

相关文章:

c# - 当行数超过 125 时,EasyUI Datagrid 行无法发布到 ASP.NET MVC4 中的相应操作

C# webapi 异步上下文问题

silverlight - 在 XAML 中创建投影样式

silverlight - 如何更改 Silverlight TextBox 中的制表符宽度

c# - 重绘 Silverlight Canvas

javascript - 为什么我的 Promise 没有异步处理?

node.js - 在 catch 中使用 wait 的效果

c# - 使用不同的数值变量类型

c# - Office VSTO Word 2003 项目不断尝试自动转换为 2007

c# - 从 DateTime 格式中减去天数