When I use this, during the time delay I can't close, maximize or minimize the program. And also move the window.
Task.Delay(10000).GetAwaiter().GetResult();
Is there another time delay that allows this to be done? Also, when I click any button on the form during the time delay, this click is counted. I need to avoid this.
That doesn't fit either
Thread.Sleep(10000);
When I use this, during the time delay I can't close, maximize or minimize the program. And also move the window.
Task.Delay(10000).GetAwaiter().GetResult();
Is there another time delay that allows this to be done? Also, when I click any button on the form during the time delay, this click is counted. I need to avoid this.
That doesn't fit either
Thread.Sleep(10000);
Share
Improve this question
asked Jan 18 at 7:53
AnutaAnuta
191 silver badge3 bronze badges
1
- 1 Fire up your search engine of choice and read what this returns you: "Steven Cleary async Task" – Fildor Commented Jan 18 at 8:10
1 Answer
Reset to default 0THe reason is that you have used blocking call .GetAwaiter().GetResult();
to wait for the delay to finish. That effectively blocks the processing thread, which then causes problems you see - unresponsive controls.
When using Task
s you should use them with async
/await
, like so:
await Task.Delay(10000);
After correctly implementing this, you will see, that during the delay the controls remain active (keep receiving events).
If you use blocking call, captured events will be processed with delay (after blocking call).
If you use asynchronous call, you will see that controls will remain responsive and process events immediately.
Then it would become clear that you need to disable controls before the delay and then reenable them (or use some flag private volatile bool _shouldProcessEvents
that would be set to false
before entering delay).