2012-10-31 78 views
2

我有一個使用異步後臺工作程序執行批處理文件的程序。下面是代碼:使用BackgroundWorker.Backgroundworker顯示命令提示符輸出

public static void Execute(CmdObj obj, bool batch) 
{ 
    CmdObj = obj; 

    var theWorker = new BackgroundWorker(); 
    theWorker.RunWorkerCompleted += WorkerCompleted; 
    theWorker.WorkerReportsProgress = true; 

    if(batch) 
    { 
     theWorker.DoWork += WorkerBatchWork; 
    }else{ 
     theWorker.DoWork += WorkerCommandWork; 
    } 
    theWorker.RunWorkerAsync(); 

} 

private static void WorkerBatchWork(object sender, DoWorkEventArgs e) 
{ 
    RunBatch(CmdObj); 
} 

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    var temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); 
    if (temp != null) 
     ProgramPath = temp.Substring(6); 

    WriteLog(CmdObj.Activity, false); 
    WriteLog(CmdObj.Error, true); 

    CmdObj.TheButton.Enabled = true; 
} 

private static void RunBatch(CmdObj obj) 
{ 
    var process = new Process(); 
    var startInfo = new ProcessStartInfo 
         { 
          FileName = obj.SrcFile, 
          WindowStyle = ProcessWindowStyle.Normal, 
          CreateNoWindow = false, 
          RedirectStandardInput = true, 
          RedirectStandardOutput = false, 
          RedirectStandardError = true, 
          UseShellExecute = false 
         }; 


    try 
    { 
     if (!obj.SrcFile.ToLower().Trim().EndsWith(".bat")) 
      throw new FileLoadException("Not a valid .bat file",obj.SrcFile); 

     process.StartInfo = startInfo; 
     process.Start(); 
     process.WaitForExit(); 

     //obj.Activity = process.StandardOutput.ReadToEnd(); 
     obj.Error = process.StandardError.ReadToEnd(); 

    } 
    catch (Exception ex) 
    { 
     obj.Exception = ex; 
    } 
    finally 
    { 
     process.Close(); 
    } 
} 

class CmdObj 
{ 
    public string Command { get; set; } 
    public string SrcFile { get; set; } 
    public string Activity { get; set; } 
    public string Error { get; set; } 
    public Exception Exception { get; set; } 
    public Button TheButton { get; set; } 
} 

所以,當我運行這個程序,並選擇一個批處理文件來執行,我得到了一個空白CMD窗口。當我的程序執行批處理文件時,有什麼方法可以在CMD窗口中顯示輸出嗎?或者,如果我可以將CMD輸出發送到文本框或其他控件(實時),那也可以。

謝謝!

+0

對不起,請您介紹一下'CmdObj'是指什麼? :) –

+0

對不起,上面的代碼更新。這只是我寫的一個容器。 – Anders

+0

非常感謝,我正在考慮:) –

回答

0

既然你已經設置爲true下列屬性之一,你會得到一個空白的黑色窗口

RedirectStandardInput 
RedirectStandardOutput 
RedirectStandardError 

這實際上會發生,因爲你告訴你的應用程序接收/發送的輸入/輸出從目標進程

我看到你試圖使用這一行

//obj.Activity = process.StandardOutput.ReadToEnd(); 

Unfortunatel擺脫批處理文件的輸出y,除非您將RedirectStandardOutput設置爲True,否則這將不起作用,以便您的應用程序能夠讀取目標批處理文件的輸出。否則,輸出將是空

private void RunBatch(CmdObj obj) 
{ 
    var process = new Process(); 
    var startInfo = new ProcessStartInfo 
    { 
     FileName = obj.SrcFile, 
     WindowStyle = ProcessWindowStyle.Normal, 
     CreateNoWindow = false, 
     RedirectStandardInput = true, 
     RedirectStandardOutput = true, //This is required if we want to get the output 
     RedirectStandardError = true, 
     UseShellExecute = false 
    }; 


    try 
    { 

     if (!obj.SrcFile.ToLower().Trim().EndsWith(".bat")) 
     throw new FileLoadException("Not a valid .bat file",obj.SrcFile); 

     process.StartInfo = startInfo; 
     process.Start(); 
     process.WaitForExit(); 

     obj.Activity = process.StandardOutput.ReadToEnd(); //Get the output from the target process 
     obj.Error = process.StandardError.ReadToEnd(); 

    } 
    catch (Exception ex) 
    { 
     obj.Exception = ex; 
    } 
    finally 
    { 
     process.Close(); //Terminate the process after getting what is required 
    } 
} 

注意UseShellExecute必須False爲了使用RedirectStandardOutput

謝謝,
我希望對您有所幫助: )

0

你不需要撥打

process.BeginErrorReadLine(); 
process.BeginOutputReadLine(); 

過程開始後? 如果我沒有記錯,我有一些問題沒有這個。