2015-10-14 59 views
0

我試圖加載一個表格適配器asynchronously。我使用了await方法。在wpf中異步加載tableadapter

的Xaml:

<ComboBox x:Name="IDComboBox" Grid.Column="1" DisplayMemberPath="ID" HorizontalAlignment="Left" Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="120" Background="White" IsEditable="True" > 
      <ComboBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <VirtualizingStackPanel/> 
       </ItemsPanelTemplate> 
      </ComboBox.ItemsPanel> 
     </ComboBox> 

代碼:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
     loadData(); 
} 
private async void loadData() 
{ 
     // Load data into the table TBLPOOL. You can modify this code as needed. 
     commercialDataSet = ((Cobra.CommercialDataSet)(this.FindResource("commercialDataSet"))); 


     var loadTblPool = Task<int>.Factory.StartNew(() => commercialDataSetTBLPOOLTableAdapter.Fill(commercialDataSet.TBLPOOL)); 
     await loadTblPool; 
     tBLPOOLViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tBLPOOLViewSource"))); 
     tBLPOOLViewSource.View.MoveCurrentToFirst(); 

} 

上述不加載數據異步,但問題是我在組合框一個ID字段。當我按下組合框來選擇ID時程序鎖定。當我調試應用程序時,出現「發生ContextSwitchDeadLock」。我看着這個錯誤,顯然它發生在一個過程花費太長時間。不知道爲什麼會發生這種情況。如果我不加載數據異步,組合框工作得很好。

+0

我對你的代碼做了一個鏡頭,從數據庫中加載了帶有31,000+個ID的組合,加載或選擇時沒有問題。除了我沒有執行'FindResource()'位...我可以問這是什麼嗎? – jsanalytics

+0

它只是所以我可以移動到第一個記錄,當它完成加載。否則一切都是空白的。感謝您的嘗試,您是否使用了相同的異步方法? – Gisiota

+0

要移動到第一條記錄,只需在組合的XAML中執行'SelectedIndex ='0'''。如果你擺脫了FindResource()位,問題就消失了嗎?是的,我使用'async/await'來加載數據。 – jsanalytics

回答

0

我通過刷新視圖來加載它。

((System.Windows.Data.CollectionViewSource)(this.FindResource("ViewSource"))).View.Refresh();