动态获取命令行输出 [英] Getting command line output dynamically
问题描述
我运行一个程序使用命令行在c#这个程序产生一些日志,而其运行需要显示这个日志,当它得到改变。我写了以下代码,但它显示所有的日志一旦进程被杀死,在运行时我的程序没有响应。我如何解决它?
关注
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(cmd,/ c+C:\\server.py);
Process proc = new Process();
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
//procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(300);
LogstextBox.Text = output;
已编辑:
,我尝试使用 OutputDataReceived
但它没有显示任何结果,这里是更改的代码:
{
//processCaller.FileName = @ping;
//processCaller.Arguments =4.2.2.4 -t;这是工作
processCaller.FileName = @cmd.exe;
processCaller.Arguments =/ c c:\\server.py; // this is not working
processCaller.StdErrReceived + = new DataReceivedHandler(writeStreamInfo);
processCaller.StdOutReceived + = new DataReceivedHandler(writeStreamInfo);
processCaller.Completed + = new EventHandler(processCompletedOrCanceled);
processCaller.Cancelled + = new EventHandler(processCompletedOrCanceled);
this.richTextBox1.Text =Server Started ..+ Environment.NewLine;
processCaller.Start();
}
private void writeStreamInfo(object sender,DataReceivedEventArgs e)
{
this.richTextBox1.AppendText(e.Text + Environment.NewLine);
}
/ p>
string output = proc.StandardOutput.ReadToEnd();
在过程终止之前,您不会进入标准输出的 p>
您应该一次读一行 - 或者只是订阅 OutputDataReceived
事件(并遵循该事件的其他要求)。
编辑:这里是适用于我的示例代码:
using System;
using System.Diagnostics;
using System.Threading;
类程序
{
public static void Main()
{
ProcessStartInfo startInfo = new ProcessStartInfo(cmd,/ c+类型Test.cs)
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
进程process = Process.Start(startInfo);
process.OutputDataReceived + =(sender,e)=> Console.WriteLine(e.Data);
process.BeginOutputReadLine();
process.Start();
process.WaitForExit();
//我们可能还没有收到所有的活动!
Thread.Sleep(5000);
}
}
请注意,在示例代码中,在任何线程上的UI调用 OutputDataReceived
处理程序 - 这对我来说是一个坏主意。
I'm running a program using command line in c# this program produce some logs while its running in need to display this logs whenever it get change. I wrote the following code but it shows all the logs once the process has been killed and during the running time my program is not responding. how can I fix it?
regards
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "C:\\server.py");
Process proc = new Process();
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
//procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(300);
LogstextBox.Text = output;
Edited:
well, I tried to use OutputDataReceived
but it doesn't show any result, here is the changed code:
{
//processCaller.FileName = @"ping";
//processCaller.Arguments = "4.2.2.4 -t"; this is working
processCaller.FileName = @"cmd.exe";
processCaller.Arguments = "/c c:\\server.py"; //this is not working
processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.Completed += new EventHandler(processCompletedOrCanceled);
processCaller.Cancelled += new EventHandler(processCompletedOrCanceled);
this.richTextBox1.Text = "Server Started.." + Environment.NewLine;
processCaller.Start();
}
private void writeStreamInfo(object sender, DataReceivedEventArgs e)
{
this.richTextBox1.AppendText(e.Text + Environment.NewLine);
}
This is the problem:
string output = proc.StandardOutput.ReadToEnd();
You won't get to the "end" of standard output until the process has terminated.
You should be reading a line at a time - or possibly just subscribing to the OutputDataReceived
event (and following the documented other requirements for that event).
EDIT: Here's sample code which works for me:
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
public static void Main()
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + "type Test.cs")
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = Process.Start(startInfo);
process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
process.BeginOutputReadLine();
process.Start();
process.WaitForExit();
// We may not have received all the events yet!
Thread.Sleep(5000);
}
}
Note that in your sample code, you're accessing the UI on whatever thread the OutputDataReceived
handler is called - that looks like a bad idea to me.
这篇关于动态获取命令行输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!