实际上我正在尝试阅读阿拉伯语本地化的pdf文件,因为每个文件花了将近5秒或更长时间来处理它,所以我想在winforms中使用后台工作程序组件来进行更新。 到目前为止,我需要在任何时间处理近200个文件。
这是我的代码,这是非常耗时的,并且在主UI线程上运行并导致UI冻结。
private void ButtonLoadBills_Click(object sender, EventArgs e)
{
foreach (var file in _filesWithPath)
{
_pdfProcessor = new PdfProcessor();
var res = _pdfProcessor.ProcessPdf(file); // Time consuming work is being done here
if ((res) && (_pdfProcessor.ProcessedDocument != null))
{
PdfDocuments.Add(_pdfProcessor.ProcessedDocument);
}
}
}
当我将此代码放在Background Worker
下时 private void ButtonLoadBills_Click(object sender, EventArgs e)
{
BwProcessPdf.RunWorkerAsync();
}
private void BwProcessPdf_DoWork(object sender, DoWorkEventArgs e)
{
var pdfDocuments = new BindingList<PdfDocumentModel>();
foreach (var file in _filesWithPath)
{
_pdfProcessor = new PdfProcessor();
var res = _pdfProcessor.ProcessPdf(file); // Time consuming work is being done here
if ((res) && (_pdfProcessor.ProcessedDocument != null))
{
pdfDocuments.Add(_pdfProcessor.ProcessedDocument);
}
BwProcessPdf.ReportProgress(1,file);
}
}
private void BwProcessPdf_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
labelControl1.Text = string.Format("{0}: {1}", PDF_PROCESSING, e.UserState);
}
private void BwProcessPdf_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{}
问题: