You can do this using a background worker fine, but you're trying to do some of the work in the process button event handler and only some in the background worker.
Try this...
* Drop a BackgroundWorker component on your form. Name it 'audioWorker'
* Click on it and choose properties
* Set WorkerReportsProgress and WorkerSupportsCancellation to true
* Click on events and create event handlers for DoWork, ProgressChanged and RunWorkerCompleted
Add a new class to your project. Call it ProcessAudioStartArguments
public class ProcessAudioStartArguments
{
public ProcessAudioStartArguments(List<ProcessStartInfo> files)
{
this.FilesToProcess = files;
}
public List<ProcessStartInfo> FilesToProcess { get; private set; }
}
Your actual code should then look something like this..
* Build up a list of arguments to send to the background worker
* Start the worker, pass in our custom arguments object
private void btnProcess_Click(object sender, EventArgs e)
{
List<ProcessStartInfo> files = new List<ProcessStartInfo>();
foreach (string loop in fileNamesToProcess)
{
Prepare(loop, out thePath, out tempStr, out thecommand, out thearguments);
ProcessStartInfo psi = new ProcessStartInfo();
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.Arguments = thearguments;
psi.FileName = thecommand;
files.Add(psi);
}
ProcessAudioStartArguments arguments = new ProcessAudioStartArguments(files);
audioWorker.RunWorkerAsync(arguments);
}
* Do all processing in the worker thread. Check for cancellations, report on progress etc
private void audioWorker_DoWork(object sender, DoWorkEventArgs e)
{
ProcessAudioStartArguments arguments = (ProcessAudioStartArguments)e.Argument;
foreach(ProcessStartInfo psi in arguments.FilesToProcess)
{
if (audioWorker.CancellationPending)
break;
var displayText = string.Format(Resources.ProcessingFile, Path.GetFileName(psi.FileName));
audioWorker.ReportProgress(step, displayText);
Process p = Process.Start(psi);
p.WaitForExit();
File.Delete(workFile);
File.Move(outFile, workFile);
}
e.Result = someObj
}
Handle progress reporting...
void audioWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string currentFile = e.UserState.ToString();
progressWindow.SetText(currentFile);
progressWindow.StepTo(e.ProgressPercentage);
}
So leave all the work to the background worker, you just start it once and pass in an arguments object that gives it all the information it requries to complete the job.
I do something similar in my article
Secure Delete .NET[
^].
HTH