在我当前的项目中,我正在使用命令提示符,并根据在 textBox 中键入的输入并按下按钮将其显示在 RichTextBox 上。
请参阅将命令提示符输出重定向到 winform 时出现 Process 类问题
我想做的一个小更新(可能不是一个特别小的更新 code-wize)是在命令提示符执行时让按钮处于“禁用”状态。由于该项目使用“Control.BeginInvoke”来更新richTextBox 上的文本,因此它是“一劳永逸”。这意味着一旦所有“BeginInvokes”都被处理到 UI 上,我就无法重新启用禁用的按钮。
我想问题是,一旦所有“BeginInvokes”都被执行并说“嘿,我完成了,这是你的按钮回来”,是否有可能获得回调。这将防止用户点击发送重复进程的按钮。
这是我正在使用的代码片段:
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
}