In my code below, I am using Process objects to execute a series of DOS batch files. I'm shortening the list of scripts for example's sake.
The subsequent (1+) scripts execute via an event handler (instead of a for loop). That way, each subsequent script runs ONLY when the previous one finishes. Now for some reason as I execute the 2nd script I can't get the caught exception to populate the status bar with the error message.
I'm testing for when invalid script names are entered in an app-config file, and I suspect I am using delegates incorrectly. My anonymous delegate consists of a combination of "new code" and existing class methods. Thats what I think is wrong; If you all could shove me toward a why I'd appreciate it ;)
NOTE: this.copy_scripts[] is constructued from a Split of: "goodname.bat,nosuchscript.bat"
private void copybutton_Click(object sender, EventArgs e)
{
InitializeBar();
this.nbr_of_copy_exits_ = 0;
this.RunCopyScript(this.nbr_of_copy_exits_);
return;
}
private void RunCopyScript(Int32 CopyScriptIdx)
{
Process proc = null;
try
{
proc = this.ObtainProcess(this.client_dest_dir_ + this.copy_scripts_[CopyScriptIdx]);
proc.EnableRaisingEvents = true;
proc.Exited += new EventHandler(CopyExited);
proc.Start();
this.progressBar.Value = ProgressInPercent(this.copy_scripts_.Count(), CopyScriptIdx);
}
catch (Exception ex)
{
this.UpdateControl(this.toolStripStatusLabel1, "Error involving " + this.copy_scripts_[CopyScriptIdx] + ": " + ex.Message);
this.copybutton.BackColor = Color.Red;
}
return;
}
void CopyExited(object sender, EventArgs e)
{
System.Diagnostics.Process senderProcess
= sender as System.Diagnostics.Process;
this.Invoke((MethodInvoker)delegate
{
if (++this.nbr_of_copy_exits_ == this.copy_scripts_.Count())
{
this.UpdateControl(this.toolStripStatusLabel1, "Copying COMPLETE.");
this.progressBar.Value = 0;
}
else
{
this.RunCopyScript(this.nbr_of_copy_exits_);
}
});
}
private void UpdateControl(ToolStripStatusLabel tssl, String text)
{
tssl.Text = text;
tssl.Refresh();
}