private void btnFix_Click(object sender, EventArgs e) { if (this.btnFix.Text == "&Run") { this.btnFix.Text = "&Stop"; this.btnScan.Enabled = false; int count = 1; foreach (ListViewItem item in lvMovies.Items) { this.lblProgress.Text = "Processing file: " + count.ToString() + " of " + filesUnprocessed.ToString(); this.statusStrip.Refresh(); if (item.ImageIndex != 0) { ThreadParams threadparams = new ThreadParams { file = item.SubItems[1].Text + "\\" + item.Text + ".mkv", index = item.Index }; Cursor.Current = Cursors.WaitCursor; lvMovies.Items[item.Index].ImageIndex = 3; this.lvMovies.Refresh(); // Start background job this.backgroundWorker1.RunWorkerAsync(threadparams); while (this.backgroundWorker1.IsBusy) { // Keep UI messages moving, so the form remains // responsive during the asynchronous operation. Cursor.Current = Cursors.WaitCursor; Application.DoEvents(); } this.lvMovies.Refresh(); Cursor.Current = Cursors.Default; count++; } if (Abort) { this.btnFix.Text = "&Run"; this.btnScan.Enabled = true; this.lblProgress.Text = ""; Abort = false; break; } } this.btnFix.Text = "&Run"; this.btnScan.Enabled = true; this.lblProgress.Text = ""; } else { // We need to cancel the running job this.backgroundWorker1.CancelAsync(); Abort = true; proc.Kill(); this.btnScan.Enabled = true; } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } ThreadParams threadparams = e.Argument as ThreadParams; int index = threadparams.index; string file = threadparams.file; if (FixFile(file, index)) { e.Result = index; } else { throw new Exception(index.ToString()); } }