Redirect process standard input in Task

别说谁变了你拦得住时间么 提交于 2019-12-25 01:25:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!