2011-03-16 34 views
0

您好,我使用多次不同參數值調用Async方法,在完成的事件中給出相同的結果。在Silverlight中多次調用異步方法問題

client.ListAllLookupValuesByTypeCompleted += client_ListAllAddressFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("AddressFormat"); 

client.ListAllLookupValuesByTypeCompleted += client_ListAllPhoneFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("PhoneFormat"); 



void client_ListAllAddressFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
     { 
      cmbAddressFormat.ItemsSource = e.Result; 
     } 


void client_ListAllPhoneFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
     { 
      cmbPhonePrintFormat.ItemsSource = e.Result; 
     } 

請幫幫我。 謝謝。

+0

當我遇到同樣的問題時,我擴展了服務客戶端類(它是一個部分類)並添加了我自己的方法,它們使用內部的BeginSomeOperation和EndSomeOperation方法。每次都不可能創建新的服務實例,因爲我需要使用會話。 – vorrtex 2011-03-16 08:38:46

+0

當有人回答您的問題時,請點擊該答案旁邊的綠色勾號。謝謝。 – jumbo 2011-03-18 23:11:41

回答

1

您可以創建client的新實例。

... 
var client = new XyzClient(); 
client.ListAllLookupValuesByTypeCompleted += client_ListAllAddressFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("AddressFormat"); 

client = new XyzClient(); 
client.ListAllLookupValuesByTypeCompleted += client_ListAllPhoneFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("PhoneFormat"); 
... 


void client_ListAllAddressFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
{ 
    cmbAddressFormat.ItemsSource = e.Result; 
} 


void client_ListAllPhoneFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
{ 
    cmbPhonePrintFormat.ItemsSource = e.Result; 
} 

另一種解決方案是在第一個處理程序中進行第二次調用(可能會創建新的客戶機實例)。

+0

感謝您的寶貴回覆。 – sag 2011-03-16 08:59:21

+0

它以這種方式工作,謝謝。 – sag 2011-03-16 09:03:03

+0

@sag不客氣!如果您接受該答案,我將不勝感激;-)謝謝。 – jumbo 2011-03-16 10:00:44