开发者

Is it possible to figure out when all Control.BeginInvoke(s) have been completed?

开发者 https://www.devze.com 2023-04-12 03:53 出处:网络
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.

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 usi开发者_StackOverflow中文版ng:

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
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号