如何正确暂停/延迟Windows Forms应用程序 [英] How to correctly pause/delay Windows Forms application

查看:81
本文介绍了如何正确暂停/延迟Windows Forms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OOP和C#的初学者.

I am a beginner to the OOP and the C#.

我正在使用Windows窗体进行问答游戏.我的问题与两个类有关,即 form 游戏逻辑.我有一个带有经典Froms控件的基本UI.看看.

I am working on a quiz game using the Windows Forms. My problem is related to two classes, the form and the game logic. I have a basic UI with classic Froms controls. Take a look.

我要实现的目标是,当玩家按下任意一个答案按钮时,它将根据红色按钮或绿色提示该按钮为红色或绿色,具体取决于答案是对还是错.更改颜色后,我希望程序等待一段时间,然后转到下一个问题.

The thing I want to achieve is, when a player presses any answer button, it will higlight that pressed button by red or green color, depending on if it is right or wrong answer. After changing the color I want the program to wait for a while and then go to the next question.

问题是,我不知道如何正确实现这一目标.我不知道如何使用线程以及Form应用程序与线程相关的工作方式.我应该使用线程睡眠还是计时器或异步?

我将向您展示游戏逻辑类中应该处理的方法.

I will show you the method in game logic class which should handle this.

public static void Play(char answer) //Method gets a char representing a palyer answer
    {
        if (_rightAnswer == answer) //If the answer is true, the button should become green
        {
            Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.LightGreen);
            _score++;
        }
        else //Otherwise the button becomes Red
        {
            Program.MainWindow.ChangeBtnColor(answer, System.Drawing.Color.Red);
        }

        //SLEEP HERE

        if (!(_currentIndex < _maxIndex)) //If it is the last question, show game over
        {
            Program.MainWindow.DisplayGameOver(_score);
        }
        else //If it is not the last question, load next question and dispaly it and finally change the button color to default
        {
            _currentIndex++;
            _currentQuestion = Database.ListOfQuestions.ElementAt(_currentIndex);
            _rightAnswer = _currentQuestion.RightAnswer;
            Program.MainWindow.DisplayStats(_score, _currentIndex + 1, _maxIndex + 1);
            Program.MainWindow.DisplayQuestion(_currentQuestion.Text);
            Program.MainWindow.DisplayChoices(_currentQuestion.Choices);
        }
        Program.MainWindow.ChangeBtnColor(answer, System.Drawing.SystemColors.ControlLight);
    }

我不想完全阻止UI,但也不想让用户通过在暂停期间按其他按钮来进行其他事件.因为这会导致应用程序运行不正常.

I don´t want to completely block the UI but also I don´t want users to make other events by pressing other buttons during the pause. Because it will result in improper run of app.

推荐答案

如果该程序非常简单,并且您不想实现线程,则建议使用Timer.单击答案"按钮时,只需启动计时器即可.您的计时器应包含一些功能,该功能会在一段时间后自行停止并执行其他需要执行的操作(例如,选择另一个问题).

If the program is really simple and you do not want to implement Threads I would suggest using Timer. Just start your Timer when clicking answer button. Your timer should contain function which would stop itself after some time and do other actions needed (e.g. pick another question).

这篇关于如何正确暂停/延迟Windows Forms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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