[英]How to run a process in loop and wait for it to complete and then run it again in C# without freezing GUI
我是 C# 編程的新手,有一段代碼我想在循環中多次運行命令行進程。 我還想等待命令行過程完成,然后使用不同的 arguments 再次運行它。 我所做的是我在一個循環中定義了該過程,並且一切正常。 但是,我也想在每次進程結束時更新 GUI 上的進度條,但最終 Process.WaitForExit() 掛起我的 GUI 並且它不會更新進度條。 當 for 循環完成時,進度條會更新。 請幫幫我,這是我一直在嘗試做的。
ProcessStartInfo processInfox = new ProcessStartInfo();
processInfox.CreateNoWindow = false;
processInfox.UseShellExecute = false;
processInfox.FileName = programPath;
processInfox.CreateNoWindow = true;
int startFrames = Convert.ToInt16(txtStartFrame.Text);
int endFrames = Convert.ToInt16(txtEndFrame.Text);
for (int i = startFrames; i <= endFrames; i++)
{
progressBar1.Value = (i / endFrames) * 100;
iName = fileFrames + "_" + i + ".jpg";
oName = outFrames + "\\" + outFileName + "_processed_" + i + ".jpg" ;
processInfox.Arguments = "-i \"" + iName + "\"" + progArgs + " -o \"" + oName + "\"";
using (Process processFire = Process.Start(processInfox))
{
processFire.WaitForExit();
}
}
EnableRaisingEvents
屬性設置為true
。 訂閱應在流程Start
之前完成。false
則調用Kill方法。new Thread
或使用ThreadPool.QueueUserWorkItem
或使用Task.Run
或...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.