Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要实时读取应用程序输出,但只有在应用程序关闭后才能接收数据_C#_C# 4.0 - Fatal编程技术网

C# 需要实时读取应用程序输出,但只有在应用程序关闭后才能接收数据

C# 需要实时读取应用程序输出,但只有在应用程序关闭后才能接收数据,c#,c#-4.0,C#,C# 4.0,我正试图在应用程序输出数据时“实时”读取控制台输出数据, 例如 当我在windows cmd.exe中运行FramworkDemo.exe时,我可以在1秒内看到字符串“hello world”, 但当我使用下面的C代码运行时,需要30秒 我只能在控制台应用程序关闭时(30秒后)接收输出数据 这就像有什么东西在锁定控制台以实时检索数据 using System; using System.Collections.Generic; using System.Diagnostics; using Sy

我正试图在应用程序输出数据时“实时”读取控制台输出数据, 例如

当我在windows cmd.exe中运行FramworkDemo.exe时,我可以在1秒内看到字符串“hello world”, 但当我使用下面的C代码运行时,需要30秒

我只能在控制台应用程序关闭时(30秒后)接收输出数据

这就像有什么东西在锁定控制台以实时检索数据

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApp14
{
    class Program
    {
        class HandleExecutable
        {
            static private DataReceivedEventHandler outputHandler;
            public DataReceivedEventHandler OutputHandler
            {
                set { outputHandler = value; }
            }
            static private DataReceivedEventHandler errorHandler;
            public DataReceivedEventHandler ErrorHandler
            {
                set { errorHandler = value; }
            }

            public void callExecutable(string executable, string args)
            {
                string commandLine = executable;
                ProcessStartInfo psi = new ProcessStartInfo(commandLine);
                psi.UseShellExecute = false;
                psi.LoadUserProfile = false;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.WindowStyle = ProcessWindowStyle.Minimized;
                psi.CreateNoWindow = true;
                psi.Arguments = args;
                Process p = new Process();
                p.StartInfo = psi;
                try
                {
                    p.Start();
                    p.BeginOutputReadLine();
                    p.BeginErrorReadLine();
                    if (outputHandler != null) p.OutputDataReceived += outputHandler;
                    if (errorHandler != null) p.ErrorDataReceived += errorHandler;
                    p.WaitForExit();
                    p.Close();
                    p.Dispose();
                }
                catch (Exception ex)
                {
                    //log.Error(ex.Message);
                }
            }
        }

        //On another class
        static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            //HANDLE STDERR
            if (e.Data != null && !e.Data.Equals(""))
            {
                Console.WriteLine(e.Data);
            }
        }

        static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
        static void Main(string[] argsmain)
        {
            HandleExecutable he = new HandleExecutable();
            he.OutputHandler = p_OutputDataReceived;
            he.ErrorHandler = p_ErrorDataReceived;
            he.callExecutable(@"C:\prod\bin\App\FramworkDemo.exe", "-cp foo ClassName");

            Console.ReadKey();
        }

    }
}