在我当前的项目中,我正在使用命令提示符,并根据在textBox中键入的输入和按下的按钮将其显示在richTextBox上。
参见Process类在重定向命令提示符输出到winform时出现问题
我想做的一个小更新(可能不是一个特别小的更新代码大小)是让按钮在命令提示符执行时处于"禁用"状态。由于项目使用了"控制"。来更新richTextBox上的文本,它会做一个"触发并遗忘"这意味着,一旦所有的"begininvoke"都被处理到UI中,我就没有办法重新启用一个被禁用的按钮。
我想问题是,是否有可能得到一个回调,一旦所有的" begininvoke "已经执行,并说"嘿,我完成了,这是你的按钮回来了。"这将防止用户点击发送重复进程的按钮。
下面是我使用的代码片段:public void GetConsoleOuput(string command = null)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
if (!string.IsNullOrEmpty(command))
{
startInfo.Arguments = command;
}
Process process = new Process();
process.StartInfo = startInfo;
process.OutputDataReceived += new DataReceivedEventHandler(AppendRichBoxText);
process.Start();
process.BeginOutputReadLine();
process.Close();
}
public void AppendRichBoxText(object sendingProcess, DataReceivedEventArgs outLine)
{
string outputString = args.Data;
MethodInvoker append = () => richTextBox.AppendText(outputString);
richTextBox.BeginInvoke(append);
}
// Would like EventHandler method to enable button once all "BeginInvokes" are
// done running asynchronously due to a callback.
public void EnableButton
{
/// re-enable a disabled button
}
在所有更新之后再次调用BeginInvoke
,然后调用EnabledButton
。