Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

asked on

C# Stop disabled button from accumulating clicks

I have a C# application with a form window that has a button on it, I'll call myButton. I have an event attached to it, myButton_Click. What I want to do is disable the button and not allow any user interface with the button while the actions within the myButton_Click method are running.

Currently I launch another executable from within the myButton_Click method and like I said I do NOT want any user interaction while the other application is running. The problem I am running into is that even though the button is disabled, by myButton.Enabled == false;, if I click multiple times on the disabled button once I close the running application that was launched from within 'myButton_Click', the method 'myButton_Click' gets recalled as many times as I clicked on the disabled button previously.

In short I would like a way to make sure that no actions/button clicks are stored/accumulated while the outside application is running on the button that I disabled.

E.g.
private void myButton_Click(object sender, EventArgs e)
{
           myButton.Enabled = false;

           ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow = false;
                startInfo.UseShellExecute = false;
                startInfo.FileName = "someapplication.EXE";

                try
                {
                    using (Process exeProcess = Process.Start(startInfo))
                    {
                        exeProcess.WaitForExit();
                    }
                }
                catch
                {
                    // Log error.
                }

            // turn the button back on
            myButton.Enabled = true;
}

Thanks
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

The button never gets disabled (at least not when you want it to).

Setting the property creates a message that the button should be disabled, but that will not happen until you exit the method and return control to the message loop. As you change the property again in the method, you have two waiting messages that will disable the button and immediately enable it again when you exit the method.

If you want the button to be disabled while the executable runs, you need to wait for the executable in a different thread, so that you don't lock the main thread.
ASKER CERTIFIED SOLUTION
Avatar of Matthew Kelly
Matthew Kelly
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Poppie12345
Poppie12345

After the myButton.Enabled = false;
Add
Application.DoEvents();