2016-08-25 105 views
0

在獲取數據的同時,似乎很長一段時間,我們無法在這段時間內完成另一項任務。我正在爲此使用BAckGroundWOrker。但似乎獲取所有數據後等待很長一段時間僅在該應用運行過程中出現細微等待很長時間從數據庫中獲取數據

private void btnExrtPDF_Click(object sender, RoutedEventArgs e) 
    { 
     btnExrtPDF.IsEnabled = false; 
     Collection.Clear();     

     long NoOfRecords = 10000; 
     long RecordsIcrease = 10000; 
     SaveFileDialog xsfd = new SaveFileDialog() 
     { 
      FileName = "Book1", 
      DefaultExt = ".xlsx", 
      Filter = "Excel Document|*.xlsx", 
      InitialDirectory = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).ToString() 
     }; 
     Nullable<bool> result = xsfd.ShowDialog(); 
     System.Data.DataTable batchFCSB = new System.Data.DataTable(); 
     int row = 0; 
     if (result == true) 
     { 

      DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy); 
      batchFCSB = DetailReportFCBuySell.ToDataTable(); 
      Collection.Add(row, batchFCSB); 
      row = 1; 
      PageIndex++; 

      for (long k = NoOfRecords; k < DetailReportFCBuySell.FirstOrDefault().TotalRecords; k = +NoOfRecords) 
      { 

        new AlxServiceClient().Using(channel => 
        { 
         DetailReportFCBuySell = new ObservableCollection<DLReports.FCBuySellDetail>(); 
         DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy); 
         batchFCSB = new System.Data.DataTable(); 
         batchFCSB = DetailReportFCBuySell.ToDataTable(); 
         Collection.Add(row, batchFCSB); 
         row++; 

       }); 
       NoOfRecords = NoOfRecords + RecordsIcrease; 
      } 


      for (int k = 0; k < Collection.Keys.Count; k++) 
      { 
       string xlsxFile = string.Empty; 
       xlsxFile = System.IO.Path.GetTempFileName();      

       TableView temp = new TableView(); 
       temp.DataContext = (Collection.Where(i => i.Key == k).FirstOrDefault().Value); 
       // ExportToXlsx(temp, xlsxFile); 
      } 
      } 
m_oWorker.RunWorkerAsync(); 
} 
+0

嗨我可以知道的是後臺工作援引在哪裏?這看起來有點直接通過使用UI線程 –

+0

m_oWorker.RunWorkerAsync(); –

+0

這是在這裏面使用的。和我在構造函數 –

回答

1

我做的EditValue到一個單獨的對象,並調用該對象的線程

DateTime startDate = Convert.ToDateTime(dateEdtStartDate.EditValue).Date; 
DateTime endDate = Convert.ToDateTime(dtpEditEndDate.EditValue).Date; 

private DetailReportFCBuySell FetchRecord() 
{ 
return ObservableCollection<DLReports.FCBuySellDetail> temp = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, startDate, endDate, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy); 
} 
0

代碼塊恍如從Click事件的開始運行,而不必調用BGWorker,考慮到代碼塊移動到一個單一的像

private DetailReportFCBuySell FetchRecord() 
{ 
    return AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy); 
} 

功能,並呼籲在m_oWorker做工作時此功能

private void bw_DoWork(object sender, DoWorkEventArgs e) 
{ 
    FetchRecord(); 
} 

閱讀更多關於https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx#code-snippet-3

+0

它使我等待我的申請 –

+0

我可以更多地瞭解情況嗎?另請考慮使用.NET中的任務,閱讀更多內容:https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx –