问题
I'm trying to start a process In C# (Java.exe running a jar file) in a seperate thread, and redirect it's StandardInput, StandardError and StandardOutput.
I've successfully redirect StandardError and StandardOutput, but I am having problems with StandardInput. I'm starting the process this way:
Action<object> MyAction = (object obj) =>
{
MyProcess.Start();
MyProcess.WaitForExit();
};
Task MyTask = new Task(MyAction);
MyTask.Start();
What I need then is to be able to, in my windows forms application, to have a textbox and a button, where I can input commands that will be sent to the process StandardInput but I cannot find a way to redirect it "outside the task", from what i'm aware it requires a streamWriter, but I cannot find a way to write to it when it is running in a seperate thread.
回答1:
You can get the StandardInput
and StandardOutput
before passing them in to the Task
:
public partial class Form1
{
private StreamWriter _streamWriter;
private StreamReader _streamReader;
public Form1()
{
InitializeComponent();
}
public void InitializeProcess(object sender, EventArgs args)
{
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
ErrorDialog = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
}
var myProcess = new Process(processStartInfo);
Task.Run(() =>
{
MyProcess.Start();
MyProcess.WaitForExit();
});
_streamWriter = MyProcess.StandardInput;
_streamReader = MyProcess.StandardOutput;
}
public void ButtonEventHandler(object sender, EventArgs e)
{
_streamWriter.Writer( /* write stuff here */ )
}
}
That way, as long as the process lives, you can access its streams. Note you will have to add a check to make sure the process is still alive, and eventually dispose your StreamWriter
and StreamReader
Edit
Instead of using a threadpool thread just to block waiting for WaitForExit
, we can register to the asynchronous Process.Exited
event:
public void InitializeProcess(object sender, EventArgs args)
{
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
ErrorDialog = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
}
var myProcess = new Process(processStartInfo);
myProcess.EnableRaisingEvents = true;
myProcess.Exited += (s, e) =>
{
// Do whatever you want when process ends, like
// Disposing of the streams.
}
myProcess.Start();
_streamWriter = MyProcess.StandardInput;
_streamReader = MyProcess.StandardOutput;
myProcess.Start();
}
回答2:
I wouldn't expect is would be easy to have StandardInput to be accessible across (parallel) tasks. Which of two parallel tasks with both access to StandardInput should get its input?
I would indeed think that you'd need some programming of buffers and/or streams.
Also, are you sure that you want to sent to StandardInput process? It typically is read only. (Though it can be the StandardOutput of some other task/process)
来源:https://stackoverflow.com/questions/25219334/redirect-process-standard-input-in-task