好的,这就是我要做的--我想编写一个.net应用程序,将标准输出/输入重定向到richtextbox。我已经让它工作得很好了,但是一旦我将标准输入添加到混合中,我的读取命令就会冻结。下面是我的表单中的相关代码。
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
//Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.ErrorDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.Start();
Timer consoleReader = new Timer();
consoleReader.Interval = 200;
consoleReader.Tick += new EventHandler(consoleReader_Tick);
consoleReader.Start();
}
void consoleReader_Tick(object sender, EventArgs e)
{
textArea.AppendText(Shell.StandardOutput.ReadToEnd());
}我也尝试过使用Process类中可用的异步读取方法来实现这一点,但是,一旦我将standardinputredirect = true添加到混合类中,它将在读取一行左右的内容后挂起。
有什么想法吗,伙计们?
[编辑]好的,这是一个示例程序。我将这段代码移到了一个控制台应用程序中,以简化一些事情。为什么这个坏了?
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace TestAsConsoleApp
{
class Program
{
static Process Shell;
static void Main(string[] args)
{
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.Start();
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.BeginOutputReadLine();
Shell.WaitForExit();
}
static void Shell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}发布于 2010-08-15 15:00:54
如果您正在进行异步读取,则不需要进行同步读取-而且您的同步代码也会中断,因为它会阻塞,直到接收到所有输出,而这不应该在UI线程中完成。如果我是你,我只会去找异步代码。
现在,你想用控制台做什么?你说标准输入导致了问题--你想写什么?您是否显式刷新?
发布于 2010-08-17 19:28:29
修改后的程序看起来不会被破坏。您创建了一个流程,请求所有要重定向的流。您将触发创建的进程输出的异步读取。那你就等着吧。在本例中,创建的cmd.exe不会在其输入流中获得任何内容,因此不会产生任何输出。可能是尝试下面的程序。运行它,并给出一些命令,如dir等,它将产生输出。希望我没有误解你的问题。
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace TestAsConsoleApp
{
class Program
{
static Process Shell;
static void Main(string[] args)
{
Shell = new Process();
Shell.StartInfo.FileName = "cmd";
Shell.StartInfo.UseShellExecute = false;
Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Shell.StartInfo.CreateNoWindow = true;
Shell.StartInfo.RedirectStandardInput = true;
Shell.StartInfo.RedirectStandardOutput = true;
Shell.StartInfo.RedirectStandardError = true;
Shell.Start();
//Shell.StandardInput.WriteLine("dir");
Shell.EnableRaisingEvents = true;
Shell.OutputDataReceived += new DataReceivedEventHandler(Shell_OutputDataReceived);
Shell.BeginOutputReadLine();
//read input from your programs input and forward that to the created cmd 's input
do
{
string aLine = Console.ReadLine();
Shell.StandardInput.WriteLine(aLine);
if (aLine.ToLower() == "exit")
break;
}while(true);
Shell.WaitForExit();
}
static void Shell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}https://stackoverflow.com/questions/3486508
复制相似问题