要啓動一個過程,而不顯示一個新的窗口,嘗試:
scriptProc.StartInfo.CreateNoWindow = true;
要顯示腳本的進步,你需要的腳本編寫進度文本到stdout,然後讓調用程序讀取進度的文字和將其顯示在用戶界面中。這樣的事情:
using (var proc = new Process())
{
proc.StartInfo = new ProcessStartInfo("cscript");
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();
proc.OutputDataReceived -= new DataReceivedEventHandler(proc_OutputDataReceived);
}
void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
var line = e.Data;
if (!String.IsNullOrEmpty(line))
{
//TODO: at this point, the variable "line" contains the progress
// text from your script. So you can do whatever you want with
// this text, such as displaying it in a label control on your form, or
// convert the text to an integer that represents a percentage complete
// and set the progress bar value to that number.
}
}
你可以附加一個偵聽程序,它每次運行cscript寫入控制檯?如果是這樣,您可以通過解析輸出來監控進度,從而更新進度條。 – 2011-07-27 12:11:07
完整源代碼示例的最終解決方案? – Kiquenet