从标准输出中以语音方式写入TextBox

时间:2018-05-21 20:37:59

标签: c#

我正在尝试获取一个按钮,在后台运行一个进程,读取输出并将其放在TextBox上。

question帮我看了输出。但是我需要 dinamically 读取输出,并且使用此解决方案,它只会在进程退出后显示完整输出。

我的代码是这样的:

private void button7_Click(object sender, EventArgs e)
{

    Process startInfo = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = "/c \"ping www.google.com\"",

            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true

        }
    };
    startInfo.EnableRaisingEvents = true;
    startInfo.OutputDataReceived += ProcessDataHandler;
    startInfo.ErrorDataReceived += ProcessDataHandler;

    m_sbText = new System.Text.StringBuilder(1000);

    startInfo.Exited += new EventHandler(this.myProcess_Exited);                

    if (MessageBox.Show("Are you sure?, this may take severa minutes", "Export DAT", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
    {
        commonMethods.logMessage(string.Concat("Exportar DAT: ", file, " ", arguments), null);
        try
        {
            startInfo.Start();
            startInfo.BeginOutputReadLine();
            while (!startInfo.HasExited)
            {
                System.Threading.Thread.Sleep(500);
                Application.DoEvents();
            }
            this.textBox1.Text = m_sbText.ToString();
        }
        catch (Exception exception)
        {
            commonMethods.logMessage(string.Concat("Error exporting DAT: "), exception.InnerException);
        }

    }
}

private static void ProcessDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        // Collect the net view command output. 
        if (!String.IsNullOrEmpty(outLine.Data))
        {
            // Add the text to the collected output.
            m_sbText.AppendLine(outLine.Data);
        }
    }

由于

0 个答案:

没有答案