private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { MyBackgroundWorker bw = sender as MyBackgroundWorker; string guid = bw.Guid.ToString(); ProgressBar pb; Label lbl; int processedFiles = e.ProgressPercentage; decimal percentageFilesProcessed = Math.Round(Decimal.Multiply(Decimal.Divide(processedFiles, bw.FileIds.Count), 100), 1); if (!string.IsNullOrEmpty(guid)) { foreach (Control c in tableLayoutPanel.Controls) { if (c.Name == ("pgb" + guid)) { pb = (ProgressBar)c; pb.Value = Math.Min(100, (int)percentageFilesProcessed); } else if (c.Name == ("lblProcessed" + guid)) { lbl = (Label)c; lbl.Text = ProcessedLabel(processedFiles, bw.FileIds.Count, percentageFilesProcessed); } } } }
private void InitializeBackgroundWorkers() { _backgroundWorkers = new MyBackgroundWorker[_numThreads]; for (int i = 0; i < _numThreads; i++) { _backgroundWorkers[i] = new MyBackgroundWorker(); _backgroundWorkers[i].DoWork += new DoWorkEventHandler(backgroundWorker_DoWork); _backgroundWorkers[i].ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); _backgroundWorkers[i].RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted); _backgroundWorkers[i].WorkerReportsProgress = true; _backgroundWorkers[i].WorkerSupportsCancellation = true; _backgroundWorkers[i].Guid = Guid.NewGuid(); _backgroundWorkers[i].ThreadId = i + 1; _backgroundWorkers[i].LogUtility = _logUtility; _backgroundWorkers[i].Log("Started"); } }
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MyBackgroundWorker bw = (sender as MyBackgroundWorker); string guid = bw.Guid.ToString(); if (e.Error != null) { bw.Log("Error: " + e.Error.Message); } else if (e.Cancelled) { bw.Log("Work Canceled"); } else { bw.Log("Work Completed"); ProgressBar pb = (ProgressBar)tableLayoutPanel.Controls.Find(("pgb" + guid), true)[0]; tableLayoutPanel.Controls.Remove(pb); } }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { MyBackgroundWorker bw = sender as MyBackgroundWorker; int numCurrentFileId = 0; for (int i = 0; i < bw.FileIds.Count; i++) { numCurrentFileId++; DataTable dt = _agileUtility.GetFileByFileId(bw.FileIds[i]); foreach (DataRow dr in dt.Rows) { MigrateFile(dr); if (bw.CancellationPending) { e.Cancel = true; return; } } bw.ReportProgress(numCurrentFileId); } }