0

我在多个列表上使用ExecuteQueryAsync将数据填充到List<ListItemCollection>。一旦收到所有响应,我将数据绑定到我的Silverlight应用程序。使用ExecuteQueryAsync时接收响应延迟或无响应

ExecuteQueryAsync理想情况下可以成功或失败。我基本上都会记录成功和失败时收到的总数答复,一旦收到的数字与总请求数相匹配。我显示一个等待图标,直到收到所有响应并且数据绑定到应用程序。现在的问题是,例如,我提出了12个请求,我只收到10或11个回应。我似乎不明白为什么我没有收到最后几个请求的回应。我既不成功也不失败。

有人遇到这个问题吗?您能否帮助我了解导致此问题的原因以及为什么回复既不是成功也不是失败。如果我执行相同的操作,它会起作用。这个问题不时发生,我不知道如何解决这个问题。

var requestCount = 0; 
var responseCount = 0; 
List<ListItemCollection>() bindingData; 
public function getData(){ 
    showWaitIcon(); 
    //Some Code here to form the CAML query 
    bindingData = new List<ListItemCollection>(); 
    for(int i=0; i<10; i++){ 
     //Some Code here to create the Client Context to query each Document Library or List 
     clientContext.RequestTimeout = -1; 
     clientContext.Load(clientContext.Web); 
     ListItemCollection _lstItems; 
     clientContext.Load(_lstItems); 
     clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed); 
     requestCount++; 
    } 
} 

private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args) 
{ 
    responseCount++; 
    this.Dispatcher.BeginInvoke(BindData); 
} 

private void onQueryFailed(object sender, ClientRequestFailedEventArgs args) 
{ 
    responseCount++; 
    this.Dispatcher.BeginInvoke(BindData); 
} 

private function BindData(){ 
    if(requestCount == responseCount() { 
     hideWaitIcon(); 
     bindToSilverlightView(bindingData); 
    } 
} 

回答

1

最可能的原因,我能想到的,没有经过你的代码看,就是你有一个同步的问题,也就是两个或两个以上的请求,在近接收结果完全相同的时间和试图修改跟踪已回答请求数量的变量。您可能需要使用“锁定”或类似的结构来确保代码块不会一次被两个线程访问。

+0

你能否给我一个你想传达的例子代码。将是非常有益 – 2013-03-14 12:15:49

+0

'私人无效onQuerySucceeded(对象发件人,ClientRequestSucceededEventArgs参数)'' {'' 锁(bindingData)'' {'' responseCount ++;'' }'' this.Dispatcher.BeginInvoke(BindData );'' }'' 私人无效onQueryFailed(对象发件人,ClientRequestFailedEventArgs参数)'' {'' 锁(bindingData)'' {'' responseCount ++;'' }'' this.Dispatcher。 BeginInvoke(BindData);' '}' – 2013-03-14 12:44:28

+0

Thnx,让我试试看看这是否有效。感谢您的帮助 – 2013-03-14 12:53:13