C#-Theard.Sleep跳过代码行

发布于 2025-01-28 15:36:54 字数 917 浏览 6 评论 0原文

我正在研究一个小C#Windows表单应用程序。 在此应用程序中,我使用thread.sleep在当前屏幕上等待,然后移至下一个屏幕。问题在于,在螺纹。SleepLine之前,我更改了某些按钮的颜色。很奇怪的是,它没有显示按钮的颜色改变了。就像它我跳过威胁。

这就是导致问题的代码:

        for(int i = 0; i < Buttons.Count; i++)
        {
            if(Buttons[i].Text == currentCard.rightAnswer)
            {
                Buttons[i].BackColor = Color.Green;
            }
            else
            {
                Buttons[i].BackColor = Color.Red;
            }
        }

        lbl_correctOrWrong.Visible = true;
        if(btn.Text == currentCard.rightAnswer)
        {
            lbl_correctOrWrong.ForeColor = Color.Green;
            lbl_correctOrWrong.Text = "Currect!";
        }
        else
        {
            lbl_correctOrWrong.ForeColor = Color.Red;
            lbl_correctOrWrong.Text = "Wrong!";
        }

        Thread.Sleep(1500);
        MoveToNextQuestion();

任何帮助将不胜感激,谢谢!

I'm working on a small C# Windows Form application.
In this application, I've used Thread.Sleep to wait on the current screen before moving to the next one. The problem is that before the Thread.Sleep line, I've changed the color of some of the buttons. What's weird is that it doesn't show that the color of the buttons changed. Just like it imedietly skips to the Threat.Sleep line.

That's the code that causes the problem:

        for(int i = 0; i < Buttons.Count; i++)
        {
            if(Buttons[i].Text == currentCard.rightAnswer)
            {
                Buttons[i].BackColor = Color.Green;
            }
            else
            {
                Buttons[i].BackColor = Color.Red;
            }
        }

        lbl_correctOrWrong.Visible = true;
        if(btn.Text == currentCard.rightAnswer)
        {
            lbl_correctOrWrong.ForeColor = Color.Green;
            lbl_correctOrWrong.Text = "Currect!";
        }
        else
        {
            lbl_correctOrWrong.ForeColor = Color.Red;
            lbl_correctOrWrong.Text = "Wrong!";
        }

        Thread.Sleep(1500);
        MoveToNextQuestion();

Any help would be much appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2025-02-04 15:36:54

Winforms应用程序中的所有UI活动默认情况下在主线程上发生(因此也称为UI线程)。

当您使用类似的语句时:

Buttons[i].BackColor = Color.Green;

它实际上将消息发送给控件以更改其颜色。
作为所有UI代码的所有其余部分,此消息执行也在主(UI)线程上。
主线程的顶级实际上是消息循环 - 从Q中提取UI消息并执行它们。

问题在于,当您使用类似的内容时:

Thread.Sleep(1500);

您在UI线程上时,由于线程正在睡觉,消息循环无法继续。

结果是颜色仅在主线程完成睡眠并恢复消息处理后发生变化。

底线:您应该避免在主线程上使用睡眠。一段时间后,您可以使用计时器进行一些操作。

All the UI activities in a WinForms application happen by default on the main thread (also called the UI thread because of that).

When you use a statement like:

Buttons[i].BackColor = Color.Green;

It actually sends a message to the control to change it's color.
As all the rest of the UI code, this message execution is done also on the main (UI) thread.
The main thread's top level is actually a message loop - extracting UI messages from a Q and executing them.

The problem is that when you use something like:

Thread.Sleep(1500);

And you are on the UI thread, the message loop cannot continue because the thread is sleeping.

The result is that the color changes only after the main thread completes the sleep, and resumes the message handling.

Bottom line: you should avoid using sleep on the main thread. You can use a timer to do some operation after some time has passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文