2011-07-27 86 views
0

行書進步,我有以下代碼:顯示在winform進度

Process scriptProc = new Process(); 
scriptProc.StartInfo.FileName = @"cscript"; 
scriptProc.Start(); 
scriptProc.WaitForExit(); 
scriptProc.Close(); 

而且我希望隱藏CSCRIPT窗口當我執行上面的代碼,將顯示。有沒有什麼辦法可以在Winform進度條控件中顯示上述腳本進度?

謝謝。

+0

你可以附加一個偵聽程序,它每次運行cscript寫入控制檯?如果是這樣,您可以通過解析輸出來監控進度,從而更新進度條。 – 2011-07-27 12:11:07

+0

完整源代碼示例的最終解決方案? – Kiquenet

回答

2

要啓動一個過程,而不顯示一個新的窗口,嘗試:

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. 

    } 
} 
+0

謝謝。你的回答讓我解決了我的問題。 –