是否有可能找出所有Control.BeginInvoke(s)何时完成?
问题描述:
在我当前的项目中,我正在使用一个命令提示符,并基于在文本框中键入的输入在richTextBox上显示它,并按下一个按钮。是否有可能找出所有Control.BeginInvoke(s)何时完成?
见Having trouble with Process class while redirecting command prompt output to winform
一个小更新我想使(可能不是一个特别小的更新代码wize)是在一个“禁用”状态的按钮时,命令提示符是做它的执行。由于该项目使用“Control.BeginInvoke”来更新richTextBox上的文本,因此它会执行“fire and forget”操作。这意味着一旦所有的“BeginInvokes”已经处理到用户界面,我可以重新启用禁用的按钮。
我想问题是,一旦所有的“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
}
答
刚过所有更新再次打电话BeginInvoke
调用EnabledButton
之后。
根据所提供的代码,我怎么知道它是什么时候“毕竟更新?” – 5StringRyan
在ProcessExit事件中。 – SLaks
它看起来像ProcessExit事件保存了一天。谢谢! – 5StringRyan