System.Diagnostics.Process 如果使用两次则不会返回控制台信息

问题描述 投票:0回答:0

我有两个进程需要一个接一个地运行,我试图将它们分成两个不同的变量,即:var process1,var process2 但即便如此,即使有终端输出,控制台也会停止在第二个进程上写入。这是代码;

    private void trainingButton_Click(object sender, EventArgs e)
    {
        var workingPath = Directory.GetCurrentDirectory();
        var openCVPath = Path.GetFullPath(ConfigSettings.OpenCVDirectory);
        var positiveSamplesPath = Path.GetFullPath(ConfigSettings.PositiveSamplesFile);
        var negativeSamplesPath = Path.GetFullPath(ConfigSettings.NegativeSamplesFile);
        int sampleCount = 0;

        //parse the data file and pull out how many samples were created during the editor run mode
        var sampleData = File.ReadAllLines(positiveSamplesPath);

        for(int i = 0; i < sampleData.Length; i++)
        {
            sampleCount += int.Parse(sampleData[i].Split(' ')[1]);
        }

        var p = new Process();
        p.StartInfo = new ProcessStartInfo
        {
            FileName = $"{openCVPath}\\opencv_createsamples.exe",
            WorkingDirectory = $"{openCVPath}",
            Arguments = $"opencv_createsamples.exe " +
                $"-info {positiveSamplesPath} " +
                $"-num {sampleCount} " +
                $"-w 24 -h 24 -vec {workingPath}\\pos.vec" +
                $"opencv_traincascade.exe " +
                $"-data {workingPath}\\{ConfigSettings.CascadeDataDirectory} " +
                $"-vec {workingPath}\\pos.vec -bg {negativeSamplesPath} " +
                $"-w 24 -h 24 -numPos {(int)(sampleCount * .9f)} -numNeg {(sampleCount / 2)} -numStages {ConfigSettings.Stages}",

            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
        WindowStyle = ProcessWindowStyle.Hidden
        };

        consoleOutputDialog.Visible= true;

        p.OutputDataReceived += WriteToConsole;
        p.Start();
        p.BeginOutputReadLine();
        p.WaitForExit();
        //p.OutputDataReceived -= WriteToConsole;
        p.Close();
        p.Dispose();

        if(DialogResult.OK == MessageBox.Show("Completed sample creations", "Sample Status", MessageBoxButtons.OK))
        {
            consoleOutputDialog.Text = "";
        }

        //get all negative images and push them to the Neg.txt file
        var negativeFiles = Directory.GetFiles(ConfigSettings.NegativeSamplesDirectory);
        File.Create(ConfigSettings.NegativeSamplesFile).Close();

        using (var writer = File.AppendText(ConfigSettings.NegativeSamplesFile))
        {
            foreach (var file in negativeFiles)
            {
                writer.WriteLine($"{workingPath}\\{file}");
            }                
        }

        p = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = $"{openCVPath}\\opencv_traincascade.exe",
                WorkingDirectory = $"{openCVPath}",
                Arguments = $"opencv_traincascade.exe " +
                $"-data {workingPath}\\{ConfigSettings.CascadeDataDirectory} " +
                $"-vec {workingPath}\\pos.vec -bg {negativeSamplesPath} " +
                $"-w 24 -h 24 -numPos {(int)(sampleCount * .9f)} -numNeg {(sampleCount / 2)} -numStages {ConfigSettings.Stages}",

                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        p.OutputDataReceived += WriteToConsole;
        p.Start();
        p.BeginOutputReadLine();
        p.WaitForExit();

        if (DialogResult.OK == MessageBox.Show("Completed training", "Training Status", MessageBoxButtons.OK))
        {
            consoleOutputDialog.Text = "";
        }
    }                                                        
    private void WriteToConsole(object sender, DataReceivedEventArgs e)
    {
        if (!String.IsNullOrEmpty(e.Data))
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                consoleOutputDialog.Text += e.Data+"\r\n";  
            }));
        }
    }

这是一个视频,展示了正在发生的事情。问题 因此,我在代码中突出显示了我的发现。第一部分没有问题,但第二部分不会打印。

c# winforms system.diagnostics
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.