我正在写一个包装CMD但我有麻烦重定向标准输入 [英] i'm writing a wrapper for cmd but am having trouble with redirecting standard input

查看:274
本文介绍了我正在写一个包装CMD但我有麻烦重定向标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行,所以这里是我在做什么 - 我想写一个.NET应用程序,重定向标准输出/输入到一个RichTextBox。我有它的工作pretty的很好,但一旦我添加标准输入到混音我读命令冻结。下面是相关code从我的表。

 贝壳=新工艺();
        Shell.StartInfo.FileName =CMD;

        Shell.StartInfo.UseShellExecute = FALSE;
        Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        Shell.StartInfo.CreateNoWindow = TRUE;

        //Shell.StartInfo.RedirectStandardInput = TRUE;
        Shell.StartInfo.RedirectStandardOutput =真;
        Shell.StartInfo.RedirectStandardError = TRUE;

        Shell.EnableRaisingEvents = TRUE;
        Shell.OutputDataReceived + =新DataReceivedEventHandler(Shell_OutputDataReceived);
        Shell.ErrorDataReceived + =新DataReceivedEventHandler(Shell_OutputDataReceived);

        Shell.Start();

        定时器consoleReader =新的Timer();
        consoleReader.Interval = 200;
        consoleReader.Tick + =新的EventHandler(consoleReader_Tick);
        consoleReader.Start();
    }

    无效consoleReader_Tick(对象发件人,EventArgs的)
    {
        textArea.AppendText(Shell.StandardOutput.ReadToEnd());
    }
 

我也试着做了Process类可用此异步读方法,但同样,当我加入standardinputredirect = true到混合,它会读取也许行左右后挂机。

任何想法家伙?

好了,这里是一个示例程序。我搬到这个code到一个控制台应用程序,以简化事情有点。这是为什么坏了?

 使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.Diagnostics程序;

命名空间TestAsConsoleApp
{
    类节目
    {
        静电工艺外壳;
        静态无效的主要(字串[] args)
        {
            壳牌=新工艺();
            Shell.StartInfo.FileName =CMD;

            Shell.StartInfo.UseShellExecute = FALSE;
            Shell.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Shell.StartInfo.CreateNoWindow = TRUE;

            Shell.StartInfo.RedirectStandardInput =真;
            Shell.StartInfo.RedirectStandardOutput =真;
            Shell.StartInfo.RedirectStandardError = TRUE;

            Shell.Start();

            Shell.EnableRaisingEvents = TRUE;
            Shell.OutputDataReceived + =新DataReceivedEventHandler(Shell_OutputDataReceived);
            Shell.BeginOutputReadLine();

            Shell.WaitForExit();
        }

        静态无效Shell_OutputDataReceived(对象发件人,DataReceivedEventArgs E)
        {
            如果(e.Data!= NULL)
                Console.WriteLine(e.Data);
        }
    }
}
 

解决方案

如果你正在读异步,你并不需要阅读同步,以及 - 和你同步code是坏了,因为它阻止,直到所有的输出已经收到,你不应该在UI线程做。我只是去异步code。如果我是你。

现在,你在想的执行的同台?你说,标准输入导致的问题 - 你有什么写呢?你明确地冲洗?

OK so here is what I'm doing -- I want to write a .net app that redirects standard out / in to a richtextbox. I've got it working pretty well, but once I add standard input into the mix my read commands freeze up. Here is the relevant code from within my form.

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

I've also tried doing this the Asynchronous reading methods available in the Process class but, again, once I add standardinputredirect = true into the mix, it will hang up after reading maybe a line or so.

Any ideas guys?

[[EDIT]] Ok, so here is a sample program. I moved this code into a console app to simplify things a bit. Why is this broken?

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

解决方案

If you're reading asynchronously, you don't need to read synchronously as well - and your synchronous code is broken, as it blocks until all the output has been received, which you shouldn't do within a UI thread. I'd just go for the asynchronous code if I were you.

Now, what are you trying to do with the console? You say that standard input is causing problems - what are you trying to write to it? Are you explicitly flushing?

这篇关于我正在写一个包装CMD但我有麻烦重定向标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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