使应用程序等待很长时间

本文关键字:长时间 等待 应用程序 | 更新日期: 2023-09-27 18:04:51

我需要从数据库中获取1000000条记录,所以我每次在循环1000条记录中获取记录,并与主集合联合。一次在1000条记录的切片中获取1000000条记录需要很长时间。在此期间,我需要在应用程序中执行另一项工作,但我的应用程序一直在等待(超过1分钟)。

你能给我一个提示,我可以做什么来采取主线程的工作?

 private void btnExrtPDF_Click(object sender, RoutedEventArgs e)
    {
        if (DetailsOrSummary == "Details")
            isDetails = true;  
        thread = new Thread(new ThreadStart(FetchRecord));
        thread.Start();
    }
 private void FetchRecord()
    {
        try
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                if (DetailsOrSummary == "Details")
                    isDetails = true;
                long NoOfRecords = 1000;
                long toFetchRecords = 1000;
                DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
                long RecordsIcrease = 1000;
                PageIndex++;
                for (long k = toFetchRecords; k < DetailReportFCBuySell.FirstOrDefault().TotalRecords; k = +toFetchRecords)
                {
                    new AlxServiceClient().Using(channel =>
                    {
                        ObservableCollection<DLReports.FCBuySellDetail> temp = AlyexWCFService.DL.DLTTIn.FCBuySELL(transactionName, isDetails, Convert.ToDateTime(dateEdtStartDate.EditValue).Date, Convert.ToDateTime(dtpEditEndDate.EditValue).Date, Customerid, ProductID, branchID, NoOfRecords, PageIndex - 1, isBuy);
                        DetailReportFCBuySell = DetailReportFCBuySell.Union(temp).ToObservableCollection();
                        PageIndex++;
                    });
                    toFetchRecords = toFetchRecords + RecordsIcrease;
                }
                ResultsData = DetailReportFCBuySell.ToDataTable();// (Collection.Where(i => i.Key == k).FirstOrDefault().Value);
                ExportToOxml(true);
            }
            else
            {
                //Other wise re-invoke the method with UI thread access
                Application.Current.Dispatcher.Invoke(new System.Action(() => FetchRecord()));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

使应用程序等待很长时间

在工作线程中,您正在检查您是否在UI线程中,否则您使用调度程序来执行UI线程中的方法。因此,从UI线程中启动一个工作线程来执行一些工作,它检查它是否在UI线程中,如果不在,则在UI线程中调用自己。

是这样的:

private void FetchRecord()
{
    // try omitted...
    if (Application.Current.Dispatcher.CheckAccess())
    {
        // do work...
    }
    else
    {
        Application.Current.Dispatcher.Invoke(new System.Action(() => FetchRecord()));
    }
}

对我来说似乎不合逻辑。

我认为解决方案是不与Dispatcher检查。你为什么要这么做?

另一个注意事项:如果您不打算添加任何内容,请删除try-catch。事实上,它是无用的。

Update from chat:

需要将非UI逻辑与UI逻辑分离。在这种情况下,您需要创建一些字段来存储UI值,这些值可以在工作线程中使用。这样的:

私有字段:

private DateTime _editStartDate; 
private DateTime _editEndDate; 

在启动工作线程之前设置它们:

_editStartDate = Convert.ToDateTime(dateEdtStartDate.EditValue).Date; 
_editEndDate = Convert.ToDateTime(dtpEditEndDate.EditValue).Date; 
// now start the thread 

并在工作线程中运行的方法中使用它们。

DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(
    transactionName, 
    isDetails, 
    _editStartDate, // use the field
    _editEndDate,   // use the field
    Customerid, 
    ProductID, 
    branchID, 
    NoOfRecords, 
    PageIndex - 1, 
    isBuy
);