重定向任务中的流程标准输入 [英] Redirect process standard input in Task

查看:45
本文介绍了重定向任务中的流程标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个单独的线程中使用 C#(运行 jar 文件的 Java.exe)启动一个进程,并重定向它的 StandardInput、StandardError 和 StandardOutput.

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.

我已经成功地重定向了 StandardError 和 StandardOutput,但是我在使用 StandardInput 时遇到了问题.我是这样开始这个过程的:

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();

然后我需要的是能够在我的 windows 窗体应用程序中有一个文本框和一个按钮,我可以在其中输入将发送到进程 StandardInput 的命令,但我找不到重定向它的方法在任务之外",据我所知,它需要一个 streamWriter,但是当它在单独的线程中运行时,我找不到写入它的方法.

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.

推荐答案

您可以在将 StandardInputStandardOutput 传递给 Task 之前获取它们代码>:

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 */ )  
    }
}

这样,只要进程存在,您就可以访问其流.请注意,您必须添加检查以确保进程仍然存在,并最终处置您的 StreamWriterStreamReader

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

编辑

我们可以注册异步Process.Exited事件,而不是使用线程池线程来阻塞等待WaitForExit:

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();
    }

这篇关于重定向任务中的流程标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆