Ich richte zum ersten Mal einen Hintergrundarbeiter ein. Es funktioniert meistens, wenn der Code läuft und meine Stop/Cancel-Taste funktioniert. Ich versuche jedoch auch, Fortschritte zu melden, um eine Fortschrittsanzeige zu aktualisieren, aber ich kann das überhaupt nicht schießen.Background Worker ReportProgress wird nicht ausgelöst
beginne ich den Code aus eine Schaltfläche klicken, die diesen Code ausgeführt wird:
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event
Meine Do_Work Methode:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int j = 0;// Count cumulative imported files
int countDupFiles = 0;// Count number of previously imported csv files
int countImportedFiles = 0;// Count imported files
foreach (string folderPath in csvDirList)
{
string[] csvFileNames = Directory.GetFiles(@folderPath, "*.csv");
frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", "");
for (int i = 0; i < csvFileNames.Length; i++, j++)
{
string csvFilePath = folderPath + "\\" + csvFileNames[i];
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
if (dataLayer.ImportCsvDataBkgrnd(this, csvFilePath, compIdValue, csvFileCount, i))//new method processes subdirectories if tick box selected
{
countImportedFiles = countImportedFiles + 1;
}
else
{
countDupFiles = countDupFiles + 1;
}
System.Threading.Thread.Sleep(500);
}
worker.ReportProgress(j);//tried using worker and backgroundWorker1 but neither works
backgroundWorker1.ReportProgress(j);
//string proj = j.ToString();
//MessageBox.Show(proj);//Displays incrementing j as expected when not commented out
}
}
if (countImportedFiles > 0)
MessageBox.Show(countImportedFiles + " files were imported.");
if (countDupFiles > 0)
MessageBox.Show(countDupFiles + " files were not imported. Matches all ready in Database.");
}
Der Versuch, eine dieser Progress Ereignisse auszulösen:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string tbProgress = (e.ProgressPercentage.ToString() + "%");
MessageBox.Show(tbProgress + "backgroundWorker1");
importProgressBar(e.ProgressPercentage);
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string tbProgress = (e.ProgressPercentage.ToString() + "%");
MessageBox.Show(tbProgress + "worker");
importProgressBar(e.ProgressPercentage);
}
Schließlich möchte ich das ProgressChanged-Ereignis diese Methode auslösen, um meine Fortschrittsbalken zu aktualisieren:
public void importProgressBar(int i)
{
progressTableLayoutPanel.Visible = true;//display progress bar
int percProgress = 100 * (i + 1)/csvFileCount;
if (percProgress <= 99)// Required to prevent values above 100 that crash the code
progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases
progressBar.Value = percProgress;
percProgressLabel.Text = percProgress.ToString();
progressTableLayoutPanel.Update();//Required to display all progress bar table contents
//Thread.Sleep(200);
if (percProgress >= 100)
{
Thread.Sleep(200);
progressTableLayoutPanel.Visible = false;
}
}
Die Schaltfläche Code aufheben, die funktioniert, sieht wie folgt aus:
private void stopImportButton_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
Die Message in meinem Progress Ereignisse nie zeigen und meine Fortschrittsbalken wird nie sichtbar gesetzt. Irgendwelche Ideen, was das Problem sein könnte?
Ich verwendete die Backgroundworker-Komponente aus der Toolbox. Aber wie in Ihrem Beispiel manuell eingerichtet, habe ich alles zum Laufen gebracht. Danke –
du bist willkommen :) –
Ich habe einen Fehler gefunden. Wenn ich meinen Importprozess mehr als einmal ausgeführt habe, wurde ein zusätzlicher Hintergrund-Worker erstellt, und beim zweiten Durchlauf wurde der gesamte Code zweimal ausgeführt, beim dritten 3 Mal usw. Die Anweisung if (bgw = null) verhindert die Erstellung von mehreren BGWs. –