Dev
Dev

Reputation: 1786

Waiting For long time Fetching the Data from DB

While Fetching the Data it seems a long time, we cant do another task during this time. am using BAckGroundWOrker for this purpose. but it seems waiting for long time after fetching all data only the application is runnig fine

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

Upvotes: 0

Views: 83

Answers (2)

Dev
Dev

Reputation: 1786

i make the editvalue to a seperate object and call that object in the thread

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);
}

Upvotes: 1

Zay Lau
Zay Lau

Reputation: 1864

Code block seems like running from the beginning of the click event without invoking the BGWorker, consider to move the code block into a single function like

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);
}

and calling this function in your m_oWorker Do Work event

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

read more on https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx#code-snippet-3

Upvotes: 0

Related Questions