C#WPF过程获得标准输出是间歇性的

2024-04-27 05:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在WPF的C文件中运行了一个Python进程:

        ProcessStartInfo StartInfo = new ProcessStartInfo(@"python ", location);
        StartInfo.UseShellExecute = false;
        StartInfo.CreateNoWindow = true;
        StartInfo.RedirectStandardOutput = true;
        StartInfo.RedirectStandardError = true;
        try
        {
            p.StartInfo = StartInfo;
            p.EnableRaisingEvents = true;
            p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
            p.Exited += new EventHandler(OnProcessExit);
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
         }
        catch (Exception exc)
        {
            Console.Write(exc);
        }

后来:

private void OnDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            torOut = e.Data;
            Console.WriteLine(torOut);
        }
    }

我的Python程序每秒打印多条消息。当我运行这个命令时,只会出现一些输出;例如,print "message"可能会被调用50次,只有在第50次之后,所有50条消息才会作为一个批出现。你知道吗

我的方法是否有问题,或者标准输出的读取速度是否特别慢/断断续续?你知道吗


Tags: 文件true消息newdata进程locationconsole