是否可以确定所有 Control.BeginInvoke 何时完成?

发布于 2024-12-09 18:00:54 字数 1630 浏览 0 评论 0原文

在我当前的项目中,我使用命令提示符,并根据在文本框中键入的输入并按下按钮将其显示在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-12-16 18:00:54

只需在完成所有更新后再次调用 BeginInvoke 即可随后调用 EnabledButton

Just call BeginInvoke again after all of your updates to call EnabledButton afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文