2

System.Threading.Thread.Sleep is good for stop time application, but it doesn't work as I need.

This is my code:

buttonShip.Enabled = false;
Saving();
SetProperties();
Loading();
ShowLabels();
System.Threading.Thread.Sleep(3000);
buttonShip.Enabled = true;

I can click buttonShip immediately after first click. When program shows the button is Disabled, its actually Enabled.

My question is, how can I stop program for 3 sec?

user123
  • 1,060
  • 3
  • 13
  • 29
Roman
  • 37
  • 1
  • 6
  • Or how can i Disable button for 3 sec – Roman Jul 10 '15 at 04:58
  • Why are you using `System.Threading.Thread.Sleep(3000);` and why do you want to disable button for just 3 seconds? – NASSER Jul 10 '15 at 05:00
  • Is this code running under `async` thread? – NASSER Jul 10 '15 at 05:01
  • Is this a Windows form (using System.Windows.Forms)? – PC Luddite Jul 10 '15 at 05:03
  • @M.NasserJavaid cz our user love clicking button two or tree times in row, when don't see program is works. And its bad for them, and for me. If dont wait 3 sec, button Enabled too fast to see. – Roman Jul 10 '15 at 05:05
  • You mean button should be enabled when required work completed? It may take 2 sec or more or may be complete in just 100 millisecond or some else – NASSER Jul 10 '15 at 05:07
  • [http://stackoverflow.com/questions/8496275/c-sharp-wait-for-a-while-without-blocking][1] – Mehul Prajapati Jul 10 '15 at 05:11
  • @M.NasserJavaid The program can be executed for 100 milliseconds, in this case, the button must be active after 3 seconds of 100 milliseconds. If program executed 5 seconds, in this case, the button must be active after 8 seconds. – Roman Jul 10 '15 at 05:12
  • execute an empty for loop for (3600x3)10800 iterations, – fhnaseer Jul 10 '15 at 05:16
  • `Timer` is the answer and link found by @MehulPrajapati looks like perfectly answering your question. If not enough - make sure to search for other Timer samples and update you post so it can be re-opened. – Alexei Levenkov Jul 10 '15 at 05:16

1 Answers1

0

Without a good, minimal, complete code example that clearly illustrates your scenario, it's hard to know for sure what you want to do.

But, it seems likely to me that what you really want is to execute the work not in the UI thread. This will allow the change in the button's Enabled state to be visible to the user, as well as allow the rest of the UI to remain responsive while the work occurs.

A basic way to accomplish that would be to change your code so it looks more like this:

async void buttonShip_Click(object sender, EventArgs e)
{
    buttonShip.Enabled = false;
    await Task.Run(() =>
    {
        Saving();
        SetProperties();
        Loading();
        ShowLabels();
    }

    // Wait for three more seconds before re-enabling the button:
    await Task.Delay(3000);

    buttonShip.Enabled = true;
}

This will cause all of your other code to execute in a different thread from the UI thread. When the task is started, the buttonShip_Click() method will actually return without having executed the very last statement. The statements in the task will execute in a different thread. When the task has completed, control will return to the buttonShip.Click() method; it will begin executing at the statement immediately following the await statement, i.e. the one that re-enables the button.

Unfortunately, your original code example is extremely vague. There is a possibility that at least some of the work in the task should also be updating the UI. If that's the case, then there are ways to accomplish that, including the simple use of Control.Invoke() to execute UI-related code on the UI thread where it belongs, or to use the Progress<T> class to accomplish the same but via the IProgress<T> interface.

If you want more advice along those lines, I recommend trying the above, research the other issues that may arise (if any), and if you are unable to solve them, post a new question regarding those new issues, being sure to include a good code example.


EDIT:

Per your comment above, quoted here:

The program can be executed for 100 milliseconds, in this case, the button must be active after 3 seconds of 100 milliseconds. If program executed 5 seconds, in this case, the button must be active after 8 seconds

I take the understanding that no matter how long the other task takes to execute, you want the button to not be re-enabled until 3 seconds later. I have updated the code example above to accomplish that.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136