C#重定向 进程调用 WinForm多线程
2008-05-12 16:00
260 查看
需求: 一个以前的控制台程序,由于命令行方式对用户不够友好,所以加个界面调用控制台程序,但是以前的控制台输出信息就要重定向到新的界面上,要不用户不知道程序信息更不好。
在命令行下重定向本来是很容易的一件事情(Hello >1.txt,Linux 的 | 管道功能更强),但是发现.net里面调用重定向却不是那么容易。
先写一个例子程序,一个Form程序,里面一个Button,每点一次Button,Console.WriteLine一些东西.
private void HelloBtn_Click(object sender, EventArgs e)
...{
Console.WriteLine(++num);
}
再写另外一个程序调用这个Hello.exe.将Hello.exe的命令行输出定向到TextBox中
private delegate void AppendRichText(string str);
public RedirectForm()
...{
InitializeComponent();
}
private void RedirectForm_Load(object sender, EventArgs e)
...{
Thread t = new Thread(new ThreadStart(PrintText));
t.Start();
}
private void PrintText()
...{
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = "Hello";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StreamReader reader = p.StandardOutput;//截取输出流
string input = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
...{
//重定向输出到TextBox中
this.Invoke(new AppendRichText(AppendText), input);//WinForm多线程的问题,不能在其他线程改变Form控件属性(除了少数属性)
input = reader.ReadLine();
}
p.WaitForExit();
}
private void AppendText(string text)
...{
this.richTextBox1.AppendText(text + " ");
}
这样也可以完成进程之间的一些信息传递。
在命令行下重定向本来是很容易的一件事情(Hello >1.txt,Linux 的 | 管道功能更强),但是发现.net里面调用重定向却不是那么容易。
先写一个例子程序,一个Form程序,里面一个Button,每点一次Button,Console.WriteLine一些东西.
private void HelloBtn_Click(object sender, EventArgs e)
...{
Console.WriteLine(++num);
}
再写另外一个程序调用这个Hello.exe.将Hello.exe的命令行输出定向到TextBox中
private delegate void AppendRichText(string str);
public RedirectForm()
...{
InitializeComponent();
}
private void RedirectForm_Load(object sender, EventArgs e)
...{
Thread t = new Thread(new ThreadStart(PrintText));
t.Start();
}
private void PrintText()
...{
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = "Hello";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
StreamReader reader = p.StandardOutput;//截取输出流
string input = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
...{
//重定向输出到TextBox中
this.Invoke(new AppendRichText(AppendText), input);//WinForm多线程的问题,不能在其他线程改变Form控件属性(除了少数属性)
input = reader.ReadLine();
}
p.WaitForExit();
}
private void AppendText(string text)
...{
this.richTextBox1.AppendText(text + " ");
}
这样也可以完成进程之间的一些信息传递。
相关文章推荐
- C#重定向 进程调用 WinForm多线程
- C#重定向 进程调用 WinForm多线程
- [C#学习]在多线程中如何调用Winform
- C# WINForm 如何关闭系统进程,调用cmd并执行命令(隐藏CMD窗口) - 赤色火焰 - 京华志
- [C#学习]在多线程中如何调用Winform[转]
- [C#学习]在多线程中如何调用Winform(转)
- [C#学习]在多线程中如何调用Winform
- c#在多线程中如何调用Winform
- C#中Invoke 和 BeginInvoke 的区别(多线程调用Winform)
- [C#学习]在多线程中如何调用Winform
- C#、VS2012 Winform程序解决方案名称更改后报错“调试器无法继续运行该进程。调用的目标发生了异常。”
- C# 在多线程中如何调用Winform
- c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程
- 在多线程中调用WinForm(C#) (转载)
- [C#,WinForm]进程输入、输出、错误流的重定向
- [C#学习]在多线程中如何调用Winform
- c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。
- c# 多线程 调用带参数函数
- 一个C#写的调用外部进程类
- c# winform调用摄像头识别二维码