2014-10-20 16 views
1

我一直在試圖讓Perl腳本放棄其輸出到C#中的列表框而沒有任何運氣。無法在C#中使用後臺工作器來獲取Perl重定向

我一直在尋找這個網站和其他人,並嘗試過多種組合,但仍然沒有任何喜悅它,也許是試圖通過後臺工作人員做的事情?

我的代碼如下,幫助將不勝感激。

public partial class Form1 : Form 
{ 
    /// <summary> 
    /// The backgroundworker object on which the time consuming operation shall be executed 
    /// </summary> 
    BackgroundWorker backgroundWorker1; 
    private String _dirPath = null; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    // On completed do the appropriate task 
    void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     btnOpenFolder.Enabled = true; 
     btnCancel.Enabled = false; 
    } 

    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     //Here you play with the main UI thread, update a progress bar or status label 
    } 

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     Process p = new Process(); 
     p.StartInfo = new ProcessStartInfo(@"c:\Perl\bin\perl.exe"); 
     p.StartInfo.WorkingDirectory = @"C:\Temp\"; 
     p.StartInfo.FileName = @"C:\Temp\dirdupes.pl"; 
     p.StartInfo.Arguments = _dirPath; 
     p.StartInfo.CreateNoWindow = true; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.RedirectStandardInput = true; 
     p.StartInfo.RedirectStandardError = true; 
     p.StartInfo.UseShellExecute = false; 
     p.Start(); 
     p.BeginErrorReadLine(); 

     //For testing 
     //p.StartInfo.FileName = "cmd.exe"; 
     //p.StartInfo.Arguments = "dir/s";  

     //For testing 
     //StreamReader sr = p.StandardError; 
     //string line; 
     //line = sr.ReadToEnd(); 
     //listBox1.Text = line; 

     using (StreamReader std_out = p.StandardOutput, std_err = p.StandardError) 
     { 
      do 
      { 
       listBox1.Items.Add(std_out.ReadToEnd()); 
       listBox1.Items.Add(std_err.ReadToEnd()); 
      } while (!std_out.EndOfStream) ; 
     } 

     p.WaitForExit(); 

     if (backgroundWorker1.CancellationPending) 
     { 
      e.Cancel = true; 
      //backgroundWorker1.ReportProgress(0); 
      return; 
     } 
     //Report 100% completion on operation completed 
     //backgroundWorker1.ReportProgress(100); 
    } 

    private void btnOpenFolder_Click(object sender, EventArgs e) 
    { 
     //Open folder browser for user to select the folder to scan 
     DialogResult result = folderBrowserDialog1.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      //Store selected folder path 
      _dirPath = folderBrowserDialog1.SelectedPath; 
     } 
     else 
     { 
      return; 
     } 

     btnOpenFolder.Enabled = false; 
     btnCancel.Enabled = true; 

     //Start the async operation here 
     backgroundWorker1.RunWorkerAsync(); 
    } 

    private void btnCancel_Click(object sender, EventArgs e) 
    { 
     if (backgroundWorker1.IsBusy) 
     { 
      //Stop/Cancel the async operation here 
      backgroundWorker1.CancelAsync(); 
     } 
    } 
} 

我結束了使用此代碼管理,以顯示Perl腳本輸出到我的列表框

void p_DataReceived(object sender, DataReceivedEventArgs e) 
    { 
     if (e.Data != null && e.Data.Length > 0) this.Invoke(new Action(() => listBox1.Items.Add(e.Data))); 
     this.Invoke(new Action(() => listBox1.TopIndex = listBox1.Items.Count - 1)); 
    } 

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     Process p = new Process(); 
     var args = new ProcessStartInfo("cmd.exe"); 
     args.WorkingDirectory = @"C:\"; 
     //p.StartInfo.FileName = "perl.exe"; 
     args.Arguments = @"/C perl.exe C:\Temp\dirdupes.pl " + _dirPath; 
     args.CreateNoWindow = true; 
     args.RedirectStandardOutput = true; 
     args.RedirectStandardInput = true; 
     args.RedirectStandardError = true; 
     args.UseShellExecute = false; 
     p = Process.Start(args); 

     p.OutputDataReceived += p_DataReceived; 
     p.ErrorDataReceived += p_DataReceived; 
     p.BeginErrorReadLine(); 
     p.BeginOutputReadLine(); 
} 
+0

出了什麼問題? – reinierpost 2014-10-20 15:57:19

+0

@reinierpost如果我將文件名更改爲可執行文件,則不輸出任何使用Perl腳本的輸出。 – Exception 2014-10-20 22:35:42

+0

是的,「文件名」應該是「程序」名稱(即,perl.exe)。看到我的完整答案。您可以提供Perl腳本作爲參數(以及您需要的任何其他內容,例如Perl腳本的參數)。 – jimtut 2014-10-20 23:41:03

回答

1

我不得不改變流程,推出這樣的:

p.StartInfo = new ProcessStartInfo(); 
    p.StartInfo.WorkingDirectory = @"C:\Temp\"; 
    p.StartInfo.FileName = @"C:\Perl\bin\perl.exe"; 
    p.StartInfo.Arguments = @"C:\Temp\dirdupes.pl"; 

到解決啓動流程的問題。也許這不是你掛斷的地方?

之後,不過,你需要拿出這條線:

p.BeginErrorReadLine(); 

在那之後,我的作品!但是,(標準)輸出的每行都將顯示爲listBox中的單個項目。也許你需要分解輸出,把每行輸出作爲一個單獨的listBox項目?

+0

嗨jimtut,我仍然無法讓它與你的例子一起工作(我必須有其他的錯誤...我會在我回到家的時候把Dropbox的Perl腳本和壓縮的項目放到Dropbox中,這很容易,已經搞砸了......我還需要它來處理用_dirPath = folderBrowserDialog1.SelectedPath – Exception 2014-10-20 23:52:41

+0

選擇的文件夾嘗試了一些不同的東西,現在它顯示到我的列表框中,取消按鈕不起作用,但我應該能夠把它整理出來。我已經添加了我原來的帖子的代碼 – Exception 2014-10-21 05:16:13

+0

很高興它的工作!如果這個答案有幫助,請接受它 – jimtut 2014-10-21 10:49:35