我有一個C#(WPF)程序來搜索目錄中的文件,並且我想要一個進度條來顯示搜索目錄中文件的進度(因爲目錄中可能有成千上萬的文件)。爲什麼我在這個C#wpf應用程序中得到這個InvalidOperationException?
我決定使用後臺工作,以便進度條正確更新(這不會發生,但這不是我最大的問題)。
目前發生的事情是它是超級怪異的作品,但大多數時候我得到這個錯誤。唯一一次我沒有得到這個錯誤是,如果我在foreach(document in myDirectory)
循環中添加一個MessageBox.Show();
來減慢速度並查看它打開的數字文件。然後它每隔一段時間就會出錯。
任何人都可以向我解釋爲什麼我得到這個錯誤,以及如何解決它?另外,如果您認爲有更好的方法來顯示進度條,請不要猶豫,以糾正我在做什麼。
我的錯誤
「System.InvalidOperationException」類型的異常出現在System.dll中,但在用戶代碼中沒有處理
附加信息:該操作已經有OperationCompleted呼籲它和進一步的電話是非法的。
myApp.xaml.cs
BackgroundWorker backgroundWorker1;
public void searchFiles()
{
//50 files in the directory
int fileCount = 50;
int documentCounter = 0;
//For every file in the directory
foreach(document in myDirectory)
{
//increment the file count
documentCounter++;
FileSearchProgressBar(documentCounter, fileCount);
}
}
private void FileSearchProgressBar(int currentDocNumber, int count)
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += (obj, ea) => CalculateProgress(currentDocNumber, count);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(progressReport);
backgroundWorker1.RunWorkerAsync();
}
void progressReport(object sender, ProgressChangedEventArgs e)
{
progBar.Value = e.ProgressPercentage;
}
private void CalculateProgress(int currentDocNumber, int count)
{
//This is where the error is reporting
backgroundWorker1.ReportProgress(currentDocNumber * (100/count));
}
EDIT1
堆棧跟蹤
System.InvalidOperationException: This operation has already had OperationCompleted called on it and further calls are illegal. at System.ComponentModel.AsyncOperation.VerifyNotCompleted() at System.ComponentModel.AsyncOperation.Post(SendOrPostCallback d, Object arg) at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress, Object userState) at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress) at HR_Search_Program.MainWindow.CalculateProgress(Int32 currentDocNumber, Int32 count) in c:\Users\myname\Documents\Visual Studio 2013\Projects\myproject\myproject\MainWindow.xaml.cs:line 589
'backgroundWorker1'從哪裏來?你試圖爲每個'document'創建一個,但沒有聲明我們不能告訴範圍。 – HABO
在我看來,跨線程異常嘗試把一個嘗試抓住,看到堆棧跟蹤和張貼在這裏,請 –
@HABO感謝您指出。我添加了聲明。 – trueCamelType