Method is executed by clicking the button despite making "button01.Enabled = false"

1

I want to disable a button while the method does not finish. I used button01.Enabled = false; , but even clicking while the button is disabled, the method is called again after it finishes.

For example, when I execute the method below and click the button three times during the three seconds it is disabled, "TEST" appears four times in output .

private void buttonSendUsers_Click(object sender, EventArgs e)
{
    Console.WriteLine("TEST");        

    buttonSendUsers.Enabled = false;

    Thread.Sleep(3000); //teste

    buttonSendUsers.Enabled = true;

    return;
}

How can I prevent the button from executing the method while disabled?

    
asked by anonymous 10.08.2017 / 18:03

1 answer

1

I found a question like this in the OS and I liked from this answer :

button1.Enabled = false;
Task.Factory.StartNew(() => {
    // execução lenta aqui, se não for lenta, não faz sentido
}).ContinueWith(task => {
    button1.Enabled = true;
}, TaskScheduler.FromCurrentSynchronizationContext());

There is another one .

    
10.08.2017 / 18:12