當執行耗時的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");
}
你知道C#4更直接地支持Python嗎? – 2012-08-09 19:21:27
我對Iron python的限制是,我使用「Arcpy」,因此將Arcpy綁定到Iron python不是很容易。 – geogeek 2012-08-09 20:30:03