是否可以确定所有 Control.BeginInvoke 何时完成?
在我当前的项目中,我使用命令提示符,并根据在文本框中键入的输入并按下按钮将其显示在 richTextBox 上。
请参阅重定向时 Process 类遇到问题命令提示符输出到 winform
我想做的一个小更新(可能不是一个特别小的代码更新)是在命令提示符执行时让按钮处于“禁用”状态这是执行。由于该项目使用“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
}
In my current project I'm taking a command prompt and pretty much displaying it on a richTextBox based on an input typed in a textBox and a button is pressed.
See Having trouble with Process class while redirecting command prompt output to winform
One small update I want to make (might not be a particularly small update code-wize) is to have the button in a "disabled" state while the command prompt is doing it's execution. Since the project uses "Control.BeginInvoke" to update the text on the richTextBox, it does a "fire and forget." This means there isn't really a way I can re-enable a disabled button once all the "BeginInvokes" have been processed to the UI.
I guess the question is, is it possible to get a callback once all the "BeginInvokes" have been executed and say "Hey I'm done, here is your button back." This will prevent a user from hitting the button sending duplicate processes.
Here is a snippet of the code I'm using:
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在完成所有更新后再次调用
BeginInvoke
即可随后调用EnabledButton
。Just call
BeginInvoke
again after all of your updates to callEnabledButton
afterwards.