C# Windows 窗体 Thread.Sleep() 不起作用? [英] C# Windows Forms Thread.Sleep() Doesn't work?

查看:53
本文介绍了C# Windows 窗体 Thread.Sleep() 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 switch (Pattern[i])
        {
            case "Blue":
                blueButton.BackColor = Color.LightBlue;
                Thread.Sleep(1000);
                blueButton.BackColor = Color.Red;
                break;
            case "Lime":
                limeButton.BackColor = Color.Aquamarine;
                Thread.Sleep(1000);
                limeButton.BackColor = Color.Red;
                break;
            case "Yellow":
                yellowButton.BackColor = ColorTranslator.FromHtml("#FFFF80");
                Task.Delay(1000).Wait();
                yellowButton.BackColor = Color.Red;
                break;
            case "Red":
                redButton.BackColor = Color.Pink;
                Task.Delay(1000);
                redButton.BackColor = Color.Red;
                break;
        }    

但是根本没有延迟……它只是跳过了.我尝试同时使用 Task.Delay 和 Thread.Sleep 但实际上它们都没有延迟.另外,我是一个新程序员,所以放轻松.

But there's simply no delay... it just jumps through. I tried using both Task.Delay and Thread.Sleep but non of them actually delay. Also I'm a new programmer so take it easy on me.

推荐答案

Thread.Sleep 确实按预期延迟了执行,但是您在打印 UI 的同一线程上运行此代码,因此没有更新可见.

Thread.Sleep does delay the execution as expected, but you run this code on the same Thread that is printing the UI therefore no Update is visible.

一个简单的解决方法是Task.Delay(你需要将该函数标记为async):

An easy fix for this is Task.Delay (you need to mark the function as async):

 case "Blue":
     blueButton.BackColor = Color.LightBlue;
     await Task.Delay(1000);
     blueButton.BackColor = Color.Red;
     break;

要标记函数为异步,请在函数调用中添加async 关键字

To mark the function async add the async keyword to the function call

 private async Task showPattern(List<string> Pattern)

您应该阅读有关 Windows GUI 线程和 异步编程的一些基础知识.

You should read some basics about windows GUI Thread and async programming.

UI 线程的一个非常基本的解释:当您调用 Form.ShowForm.ShowDialog 时,应用程序正在启动所谓的消息循环.此消息循环确实处理所显示窗口的鼠标和键盘事件.这个消息循环也处理窗口的绘制.因此,如果此线程被锁定,您的应用程序将无响应.按钮单击处理程序(以及您窗口的所有其他处理程序)由同一个线程处理.如果您在此处理程序之一中阻止执行(就像您对 Thread.Delay 所做的那样),则此时窗口将无响应(不处理事件且不重绘).

A very basic explanation of the UI Thread: When you call Form.Show or Form.ShowDialog the application is starting a so called message loop. This message loop does process mouse and keyboard events of the window shown. This message loop does aswell handle the drawing of the window. Therefore if this thread is locked up, you application becomes unresponsive. A Button Click handler (and all other handlers of you Window) are processed by this same Thread. If you block the execution in one of this handlers (as you did with Thread.Delay) the window is unresponsive (not processing events and not redrawing) in this Time.

要克服这个问题,一种解决方案是使用 asyncawait .这两种方法做了很多编译器魔法,使这项工作如预期的那样工作(查看链接了解更多信息,你绝对应该这样做!)

To overcome this issue one solution is to work with async and await . This two methods do a lot of compiler magic to make this work as one would expect (check the link for more information, you definitely should!)

这篇关于C# Windows 窗体 Thread.Sleep() 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆