at runtime of a python script, I would control the IU with a background worker to display a progress bar.
I have successfully used a background worker when I do not need an event OutputDataReceived, but the script that I use prints some progress values, such as ("10", "80", ..), so I had to listen to the event OutputDataReceived.
I get this error: This operation has already had OperationCompleted called on it and further calls are illegal.on this line progress.bw.ReportProgress(v);.
I tried to use 2 instances of the desktop, one of them executed, and the other listens, it does not give any errors, but it does not seem to raise the OutputDataReceived event, so I do not see any progress in the progress bar.
below is the code i used:
private void execute_script()
{
progress.bw.DoWork += new DoWorkEventHandler(
delegate(object o, DoWorkEventArgs args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "python.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = @".\scripts\script1.py " + file_path + " " + txtscale.Text;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardError = true;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
});
progress.bw.RunWorkerAsync();
}
void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (e.Data != null)
{
int v = Convert.ToInt32(e.Data.ToString());
MessageBox.Show(v.ToString());
progress.bw.ReportProgress(v);
}
else
MessageBox.Show("null received");
}