【问题标题】:Process output doesn't write last line进程输出不写最后一行
【发布时间】:2016-06-25 13:40:16
【问题描述】:

我正在为控制台程序制作 GUI。有时它需要像密码这样的输入,所以如果输出包含“密码”,它会打开一个对话框。

问题是输出没有打印“输入您的密码”行,而是运行控制台。

开幕...
读取配置...
输入您的密码:            

流程:

this.ASFProcess.StartInfo.Arguments = "--server";
this.ASFProcess.StartInfo.CreateNoWindow = true;
this.ASFProcess.StartInfo.Domain = "";
this.ASFProcess.StartInfo.FileName = "ASF.exe";
this.ASFProcess.StartInfo.LoadUserProfile = false;
this.ASFProcess.StartInfo.Password = null;
this.ASFProcess.StartInfo.RedirectStandardError = true;
this.ASFProcess.StartInfo.RedirectStandardInput = true;
this.ASFProcess.StartInfo.RedirectStandardOutput = true;
this.ASFProcess.StartInfo.StandardErrorEncoding = null;
this.ASFProcess.StartInfo.StandardOutputEncoding = null;
this.ASFProcess.StartInfo.UserName = "";
this.ASFProcess.StartInfo.UseShellExecute = false;
this.ASFProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
this.ASFProcess.SynchronizingObject = this;
this.ASFProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(this.ASFProcess_OutputDataReceived);
this.ASFProcess.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(this.ASFProcess_ErrorDataReceived);

方法:

private void BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    ASFProcess.Start();
    ASFProcess.BeginOutputReadLine();
    ASFProcess.BeginErrorReadLine();
    ASFProcess.WaitForExit();
}

private void ASFProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    rtbOutput.AppendText(e.Data + Environment.NewLine);
    rtbOutput.SelectionStart = rtbOutput.Text.Length;
    rtbOutput.ScrollToCaret();
    if (e.Data.Contains("password"))
    {
        var enterPassword = new EnterPassword();
        enterPassword.ShowDialog();
    }
}

【问题讨论】:

    标签: c# process


    【解决方案1】:

    据此:https://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived(v=vs.110).aspx

    OutputDataReceived 事件表明关联的进程有 写了一个

    行,以换行符结尾,到它的重定向 标准输出流。

    所以我建议 ASF.exe 程序的问题在于,在它提示输入密码的行上,它没有在需要输入之前发送换行符。

    我建议更改 ASF 程序的工作方式,或尝试以其他方式从标准和错误输出流中读取。

    这可能会对您有所帮助:

    https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多