最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Need a slightly different time delay - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

THe 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 Tasks 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).

发布评论

评论列表(0)

  1. 暂无评论