【发布时间】:2016-08-31 06:54:22
【问题描述】:
我正在使用带有BackgroundWorker的线程,应用程序等待很长时间来获取50000条记录,它使其他操作等待该线程完成。使用辅助线程时如何避免等待过程。
private void btnExrtPDF_Click(object sender, RoutedEventArgs e)
{
if (DetailsOrSummary == "Details")
isDetails = true;
Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
{
try
{
DetailReportFCBuySell = AlyexWCFService.DL.DLTTIn.FCBuySELL(
transactionName, isDetails,
Convert.ToDateTime(dateEdtStartDate.EditValue).Date,
Convert.ToDateTime(dtpEditEndDate.EditValue).Date,
Customerid, ProductID, branchID,
NoOfRecords, PageIndex - 1, isBuy);
worker.RunWorkerAsync();
}
catch
{
object obj = new object();
}
}));
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(new Action(() =>
{
System.Data.DataTable batchFCSB = new System.Data.DataTable();
int row = 0;
if (DetailReportFCBuySell.FirstOrDefault().TotalRecords > toFetchRecords)
{
long RecordsIcrease = 1000;
batchFCSB = DetailReportFCBuySell.ToDataTable();
//Collection.Add(row, batchFCSB);
row = 1;
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();
row++;
PageIndex++;
});
toFetchRecords = toFetchRecords + RecordsIcrease;
}
}
}), DispatcherPriority.ContextIdle);
}
【问题讨论】:
-
只要确保更新 UI 组件和操作 observable 集合是在 UI 线程上完成的。目前,您正在 UI 线程上执行所有操作。
-
s 已经是我解决的问题了,感谢您对我的帮助。
标签: c# multithreading c#-4.0 backgroundworker