C#从重定向的StandardOutput异步读取数据

时间:2019-05-09 20:51:41

标签: c# asynchronous redirectstandardoutput

我正在开发C#WFA,它允许我使用化学软件GAMESS做一些事情。 基本上,我有一个批处理文件,当使用适当的参数执行该批处理文件时,该文件将返回包含原子analisys所需数据的文件。

在我的应用程序表单中,我使用流程的输出设置了一个只读文本框。我通过重新定义标准输出来做到这一点。此读取以同步方式完成。对于小输出,效果很好,但很多时候线路焊接超过240k。我的进程在结束时自动关闭(最后一条指令是“ EXIT”,而不是“ EXIT / B”)。 您已经(可能)发现主表单变得不稳定,并且在过程结束之前不允许任何用户输入。这是一个问题...

我已经尝试在代码中异步读取FileStream的另一个函数,并且工作得很好,但是我对StandardOutput感到困惑。我想不通正确的方法。

到目前为止,这里是我的代码:

private void Button1_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Input_job_file) && !String.IsNullOrEmpty(Log_file))
            {
                textBox3.Text = "Executing code...\r\n";
                string outtext;
                string batpath = string.Format(Settings.Default.path_to_gamess + "\\rungms.bat");
                string arguments = string.Format("{0} {1} {2} 0 {3}", Input_job_file, Settings.Default.version, Ncpus, Log_file);
                //Here we need to copy the input file to the gamess directory in order to avoid errors
                File.Copy(Input_job_file, Settings.Default.path_to_gamess + "\\" + FileNameWithoutExtension + ".inp");
                Process gamessjob = new Process();
                gamessjob.StartInfo.ErrorDialog = true;
                gamessjob.StartInfo.UseShellExecute = false;
                gamessjob.StartInfo.CreateNoWindow = true;
                gamessjob.StartInfo.RedirectStandardOutput = true;
                gamessjob.StartInfo.RedirectStandardError = true;
                gamessjob.EnableRaisingEvents = true;
                gamessjob.StartInfo.WorkingDirectory = Settings.Default.path_to_gamess;
                gamessjob.StartInfo.FileName = batpath;
                gamessjob.StartInfo.Arguments = arguments; //input_file, version, number_of_processors, "0", output_name]
                gamessjob.Start();

            //STDOUT redirection to our textbox in readonly mode
            outtext = gamessjob.StandardOutput.ReadToEnd();
            textBox3.Text += outtext + "\r\nProcess executed!";

            //here we clean up some stuff after GAMESS code ends
            File.Delete(Settings.Default.path_to_gamess + "\\" + FileNameWithoutExtension + ".inp");
            MessageBox.Show("Code executed!\nCheck output for errors or messages.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else MessageBox.Show("Input file and/or output location is invalid!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

//ASYNC
    byte[] result;
    textBox6.Text = "Executing code...\r\n\n";
    using (FileStream SourceStream = File.Open(Input_dat_file, FileMode.Open))
    {
        result = new byte[SourceStream.Length];
        await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
    }

理想情况下,以异步方式读取重定向的标准输出应该可以解决我应用程序的死块(对吗?),或者至少可以减少其持续时间。也许如果我使用ReadLineAsync()而不是ReadToEndAsync()进行操作,则文本框将更频繁地进行更新,因此更加“令人愉悦”。 我已经在教程中搜索了异步操作,但是所有这些教程都显示了基于文件的操作。依靠Microsoft参考获得重定向标准输出和异步信息,这让我更加困惑。

有人可以帮我找出一种使用ReadLineAsync()逐行读取输出的方法吗?

1 个答案:

答案 0 :(得分:2)

知道了。下面是有效的代码。这样,文本框将逐行更新,并且不再出现死区。

public async void Button1_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Input_job_file) && !String.IsNullOrEmpty(Log_file))
        {
            textBox3.Text = "Executing code...\r\n";
            string outtext;
            string batpath = string.Format(Settings.Default.path_to_gamess + "\\rungms.bat");
            string arguments = string.Format("{0} {1} {2} 0 {3}", Input_job_file, Settings.Default.version, Ncpus, Log_file);
            //Here we need to copy the input file to the gamess directory in order to avoid errors
            File.Copy(Input_job_file, Settings.Default.path_to_gamess + "\\" + FileNameWithoutExtension + ".inp");
            Process gamessjob = new Process();
            gamessjob.StartInfo.ErrorDialog = true;
            gamessjob.StartInfo.UseShellExecute = false;
            gamessjob.StartInfo.CreateNoWindow = true;
            gamessjob.StartInfo.RedirectStandardOutput = true;
            gamessjob.StartInfo.RedirectStandardError = true;
            gamessjob.EnableRaisingEvents = true;
            gamessjob.StartInfo.WorkingDirectory = Settings.Default.path_to_gamess;
            gamessjob.StartInfo.FileName = batpath;
            gamessjob.StartInfo.Arguments = arguments; //input_file, version, number_of_processors, "0", output_name]
            gamessjob.Start();

            //STDOUT redirection to our textbox in readonly mode
            while((outtext = await gamessjob.StandardOutput.ReadLineAsync()) != null)
            {
                textBox3.Text += outtext + "\r\n";
            }
            textBox3.Text += "\r\nProcess executed!";
            //here we clean up some stuff after GAMESS code ends
            File.Delete(Settings.Default.path_to_gamess + "\\" + FileNameWithoutExtension + ".inp");
            MessageBox.Show("Code executed!\nCheck output for errors or messages.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else MessageBox.Show("Input file and/or output location is invalid!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }