在执行耗时的 python 脚本时,我会使用后台工作人员管理 IU 以显示进度条。
当我不需要事件时,我已经成功地使用了后台工作人员OutputDataReceived
,但是我正在使用的脚本会打印一些进度值,例如(“10”,“80”,..),所以我必须听事件OutputDataReceived
。
我得到这个错误:This operation has already had OperationCompleted called on it and further calls are illegal.
在这一行progress.bw.ReportProgress(v);
。
我尝试使用 2 个后台工作实例,一个执行,另一个侦听,它没有给出错误,但似乎没有调用事件“OutputDataReceived”,所以我在进度条中看不到任何进展。
在我使用的代码下方:
private void execute_script()
{
progress.bw.DoWork += new DoWorkEventHandler( //progress.bw is reference to the background worker instance
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.CreateNoWindow = true;
//proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.RedirectStandardOutput = true;
//proc.EnableRaisingEvents = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardError = true;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
//proc.WaitForExit();
//proc.Close();
});
progress.bw.RunWorkerAsync();
}
///the function called in the event OutputDataReceived
void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
//throw new NotImplementedException();
if (e.Data != null)
{
int v = Convert.ToInt32(e.Data.ToString());
MessageBox.Show(v.ToString());
// report(v);
progress.bw.ReportProgress(v);
}
else
MessageBox.Show("null received");
}