实时C#cmd输出 [英] C# cmd output in real time

查看:165
本文介绍了实时C#cmd输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用隐藏控制台CMD的程序。从编解码器 ffmpeg开始。

I am writing a program that uses a hidden console CMD. Starts in the codec "ffmpeg". When converting all the time gets feedback.

我希望控制台每1秒产生一次充电。不幸的是,互联网上所有可用的代码都遵循转换后可以退款的原则,我想一直在观察着您的情况。

I wish every 1 second charge results from the console. Unfortunately, all the codes available on the internet work on the principle that gets refund after the conversion and I want to be watching you all the time what was happening.


public void ExecuteCommandSync(object command)
{
     try
     {
         // create the ProcessStartInfo using "cmd" as the program to be run,
         // and "/c " as the parameters.
         // Incidentally, /c tells cmd that we want it to execute the command that follows,
         // and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
      }
      catch (Exception objException)
      {
      // Log the exception
      }
} 


推荐答案

如果要不等待就获取输出,请使用异步功能。
Checkout http://msdn.microsoft。 com / zh-CN / library / system.diagnostics.process.beginoutputreadline.aspx

If you want to get the output without waiting use the async features. Checkout http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

设置事件处理程序

proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};

开始异步读取标准输出

proc.BeginOutputReadLine();

我已修改您的代码以使用这些功能。

I have modified your code to use these features.

    public static void ExecuteCommandSync(object command)
    {
        try
        {
            // create the ProcessStartInfo using "cmd" as the program to be run,
            // and "/c " as the parameters.
            // Incidentally, /c tells cmd that we want it to execute the command that follows,
            // and then exit.
            var procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);


            // The following commands are needed to redirect the standard output.
            // This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            var proc = new System.Diagnostics.Process();
            proc.OutputDataReceived += (s,e) => { Console.WriteLine(e.Data);};
            proc.StartInfo = procStartInfo;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.WaitForExit();
        }
        catch (Exception objException)
        {
            Console.WriteLine("Error: " + objException.Message);
            // Log the exception
        }
    }

这篇关于实时C#cmd输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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