C#的process类执行本地程序是怎样输出一行就被马上获取 c#异步process 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用StreamReader SR=P.StanderdOutput;while(!SR.EndOfStream){string s=SR.ReadLine();} 猜测在写的一方可能要写一句flush一句,否则写的东西在buffer里,可能不是马上能读到 调用的方式不对吧完整代码:richTextBox1.Text = ""; string[] commandlist = textBox1.Text.Split(' '); string command = string.Empty; for (int i = 1; i < commandlist.Length; i++) { command += commandlist[i] + " "; } ProcessStartInfo start = new ProcessStartInfo(commandlist[0] + ".exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe start.Arguments = command;//设置命令参数 start.CreateNoWindow = true;//不显示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p = null; StreamReader reader = null; try { p = Process.Start(start); reader = p.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 while (!reader.EndOfStream) { if (line.Length > 0) { richTextBox1.AppendText(line + "\n"); } line = reader.ReadLine(); if (backgroundWorker1.CancellationPending) { break; } } //p.WaitForExit();//等待程序执行完退出进程 } catch(Exception ex) { richTextBox1.Clear(); richTextBox1.Text = "无效指令"; } finally { try { p.Close();//关闭进程 reader.Close();//关闭流 } catch { } } 我知道了,你在主线程里while,阻塞了UI显示了要么放到backgroudworker里(多线程)执行要么每次给text赋值,要执行Application.DoEvents()让UI刷新 我的process是在子线程里执行的private void tlbtn_run_Click(object sender, EventArgs e) { this.tlbtn_run.Enabled = false; this.rtxtOutput.Text = string.Empty; // run python program ThreadStart run_py_dele = new ThreadStart(RunPy); Thread run_py_process = new Thread(run_py_dele); run_py_process. } public void RunPy() { SetButtonEnabel setEnable = new SetButtonEnabel(SetBtnEanbel); SetTreeViewNodeImageIndex setTreeNodeImage = new SetTreeViewNodeImageIndex(SetTreeNodeImageIndex); string filename = "python.exe"; string[] _args = new string[2]; Process p_run = new Process(); p_run.StartInfo.FileName = filename; p_run.StartInfo.UseShellExecute = false; p_run.StartInfo.WorkingDirectory = Environment.CurrentDirectory; p_run.StartInfo.RedirectStandardInput = true; p_run.StartInfo.RedirectStandardOutput = true; p_run.StartInfo.RedirectStandardError = true; p_run.StartInfo.CreateNoWindow = true; //p_run.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler); //p_run.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler); proc_output = new StringBuilder(); TreeNode root = this.treev_case_list.Nodes[0]; //模块级别 foreach (TreeNode tnode in root.Nodes) { //用例级别 foreach (TreeNode ttnode in tnode.Nodes) { error_flag = 0; if (ttnode.Checked == true && ttnode.Tag.ToString() != "dir") { _args = get_Script_config(ttnode.Tag.ToString()); p_run.StartInfo.Arguments = string.Format("{0} -f {1}", _args[0], Environment.CurrentDirectory + "\\" + _args[1]); p_run.Start(); StreamReader sr = p_run.StandardOutput; while (!sr.EndOfStream) { LogOutput(sr.ReadLine()); } StreamReader esr = p_run.StandardError; while (!esr.EndOfStream) { error_flag = 1; LogError(esr.ReadLine()); } //if (flag == 0) //{ // p_run.BeginOutputReadLine(); // p_run.BeginErrorReadLine(); // flag = 1; //} //p_run.WaitForExit(); //if (proc_output.Length > 0) //{ // LogOutput(proc_output.ToString()); //} //err_str = p_run.StandardError.ReadToEnd(); //if (!string.IsNullOrEmpty(err_str)) //{ // LogError(err_str); //} p_run.Close(); if (error_flag == 1) { this.Invoke(setTreeNodeImage, ttnode, 2); } else { this.Invoke(setTreeNodeImage, ttnode, 1); } } } } this.Invoke(setEnable, new object[] { true}); } public void LogAppend(Color color, string text) { this.rtxtOutput.SelectionColor = color; this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text))); this.rtxtOutput.AppendText("\n"); }主线程负责启动子线程,然后所有的操作都在子线程里。上面是相关代码,请指正。多谢 LogOutput的代码在哪呢,是直接输出了,还是保存到哪里去了? public void LogOutput(string text) { LogAppendDelegate la = new LogAppendDelegate(LogAppend); this.rtxtOutput.Invoke(la, Color.Black, text); }public void LogAppend(Color color, string text) { this.rtxtOutput.SelectionColor = color; this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text))); this.rtxtOutput.AppendText("\n"); Application.DoEvents(); }到这里来了Application.DoEvents();加了这个也没用 这个代码放到cmd里执行,是一行一行的出来,还是一下出来500行? 至少在cmd里,是一行一行出来,你程序才能一行一行的读如果cmd里就是一下出来500行,你这边做啥都没用. 其实我想做的是这个,一个自动化测试工具,当我执行脚本的时候会有很多信息输出,之前都是在linux下运行的,但是很多人都喜欢GUI。所以想做一个GUI的运行工具,脚本每输出一行就在工具的下面打印一行。现在的情况是脚本执行完后,所有的信息才一次打印出来 碰到个VS2008添加引用的问题 如何更快完成软件开发 如何为Color赋值 水晶报表求列A最大值所在行的列B的值 如何动态选择access数据库中的表,把表里的数据输入到水晶报表里。(急) 调用excel 打印翻页 怎么学习,先听听各位的高见 求助!不知怎么描述,请看代码! 谁有王凯明的“浅析C#套接字编程”的源程序,给我发一个?100分 FTPComm控件在那里找? c#中读取其它项目的配置文件问题 C#导出Excel设置页眉的字体、粗细、大小、颜色、背景色?
StreamReader SR=P.StanderdOutput;
while(!SR.EndOfStream)
{
string s=SR.ReadLine();
}
完整代码:richTextBox1.Text = "";
string[] commandlist = textBox1.Text.Split(' ');
string command = string.Empty;
for (int i = 1; i < commandlist.Length; i++)
{
command += commandlist[i] + " ";
}
ProcessStartInfo start = new ProcessStartInfo(commandlist[0] + ".exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = command;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p = null;
StreamReader reader = null;
try
{
p = Process.Start(start);
reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
if (line.Length > 0)
{
richTextBox1.AppendText(line + "\n");
}
line = reader.ReadLine();
if (backgroundWorker1.CancellationPending)
{
break;
}
}
//p.WaitForExit();//等待程序执行完退出进程
}
catch(Exception ex)
{
richTextBox1.Clear();
richTextBox1.Text = "无效指令";
}
finally
{
try
{
p.Close();//关闭进程
reader.Close();//关闭流
}
catch { }
}
要么放到backgroudworker里(多线程)执行
要么每次给text赋值,要执行Application.DoEvents()让UI刷新
private void tlbtn_run_Click(object sender, EventArgs e)
{
this.tlbtn_run.Enabled = false;
this.rtxtOutput.Text = string.Empty;
// run python program
ThreadStart run_py_dele = new ThreadStart(RunPy);
Thread run_py_process = new Thread(run_py_dele);
run_py_process.
}
public void RunPy()
{
SetButtonEnabel setEnable = new SetButtonEnabel(SetBtnEanbel);
SetTreeViewNodeImageIndex setTreeNodeImage = new SetTreeViewNodeImageIndex(SetTreeNodeImageIndex);
string filename = "python.exe";
string[] _args = new string[2];
Process p_run = new Process();
p_run.StartInfo.FileName = filename;
p_run.StartInfo.UseShellExecute = false;
p_run.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
p_run.StartInfo.RedirectStandardInput = true;
p_run.StartInfo.RedirectStandardOutput = true;
p_run.StartInfo.RedirectStandardError = true;
p_run.StartInfo.CreateNoWindow = true;
//p_run.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
//p_run.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
proc_output = new StringBuilder();
TreeNode root = this.treev_case_list.Nodes[0];
//模块级别
foreach (TreeNode tnode in root.Nodes)
{
//用例级别
foreach (TreeNode ttnode in tnode.Nodes)
{
error_flag = 0;
if (ttnode.Checked == true && ttnode.Tag.ToString() != "dir")
{
_args = get_Script_config(ttnode.Tag.ToString());
p_run.StartInfo.Arguments = string.Format("{0} -f {1}", _args[0], Environment.CurrentDirectory + "\\" + _args[1]);
p_run.Start();
StreamReader sr = p_run.StandardOutput;
while (!sr.EndOfStream)
{
LogOutput(sr.ReadLine());
}
StreamReader esr = p_run.StandardError;
while (!esr.EndOfStream)
{
error_flag = 1;
LogError(esr.ReadLine());
}
//if (flag == 0)
//{
// p_run.BeginOutputReadLine();
// p_run.BeginErrorReadLine();
// flag = 1;
//}
//p_run.WaitForExit();
//if (proc_output.Length > 0)
//{
// LogOutput(proc_output.ToString());
//} //err_str = p_run.StandardError.ReadToEnd();
//if (!string.IsNullOrEmpty(err_str))
//{
// LogError(err_str);
//} p_run.Close();
if (error_flag == 1)
{
this.Invoke(setTreeNodeImage, ttnode, 2);
}
else
{
this.Invoke(setTreeNodeImage, ttnode, 1);
}
}
}
}
this.Invoke(setEnable, new object[] { true});
} public void LogAppend(Color color, string text)
{
this.rtxtOutput.SelectionColor = color;
this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)));
this.rtxtOutput.AppendText("\n");
}
主线程负责启动子线程,然后所有的操作都在子线程里。上面是相关代码,请指正。多谢
public void LogOutput(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.rtxtOutput.Invoke(la, Color.Black, text);
}public void LogAppend(Color color, string text)
{
this.rtxtOutput.SelectionColor = color;
this.rtxtOutput.AppendText(Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)));
this.rtxtOutput.AppendText("\n");
Application.DoEvents();
}到这里来了
Application.DoEvents();加了这个也没用
如果cmd里就是一下出来500行,你这边做啥都没用.
其实我想做的是这个,一个自动化测试工具,当我执行脚本的时候会有很多信息输出,之前都是在linux下运行的,但是很多人都喜欢GUI。所以想做一个GUI的运行工具,脚本每输出一行就在工具的下面打印一行。现在的情况是脚本执行完后,所有的信息才一次打印出来