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.