我一直在試圖讓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();
}
出了什麼問題? – reinierpost 2014-10-20 15:57:19
@reinierpost如果我將文件名更改爲可執行文件,則不輸出任何使用Perl腳本的輸出。 – Exception 2014-10-20 22:35:42
是的,「文件名」應該是「程序」名稱(即,perl.exe)。看到我的完整答案。您可以提供Perl腳本作爲參數(以及您需要的任何其他內容,例如Perl腳本的參數)。 – jimtut 2014-10-20 23:41:03