C# 在不暂停GUI的情况下请求用户输入的最佳方法?

C# 在不暂停GUI的情况下请求用户输入的最佳方法?,c#,winforms,C#,Winforms,我有一个简单的问答式程序,其中有一些代码如下: private void AskQuestion(Question q) { questionbox.Text = q.GetQuestion(); answering = true; while (answering == true) { } if (q.GetQuest

我有一个简单的问答式程序,其中有一些代码如下:

  private void AskQuestion(Question q)
        {
            questionbox.Text = q.GetQuestion();
            answering = true;

            while (answering == true)
            {

            }

                if (q.GetQuestion() == answerbox.Text)
                {
                    MessageBox.Show("well done");
                }

                else
                {
                    MessageBox.Show("nope");
                }

        }
回答只是一个开关,我有这样的程序不会测试的答案,直到用户把答案,并点击一个按钮

我有一个供用户单击的按钮,可将其切换为false:

private void Answer_Click(object sender, EventArgs e)
        {
            answering = false; 
        }
其思想是while循环暂停程序,并在用户回答问题后退出,但它只是冻结整个过程

我试着用线程睡眠来减慢它的速度,然后我用定时器观察变量,在一个新的线程上尝试,但是线程之间不会相互交谈,所以我陷入了一个愚蠢的境地,我被卡住了

请帮助程序员,并在此为我建议一个策略?

以下是示例:

        private void button2_Click(object sender, EventArgs e)
        {
            hey = true;
            Thread thread = new Thread(new ThreadStart(AskQuestion));
            thread.Start();
        }

        bool hey;
        void AskQuestion()
        {
            while (hey)
            { 

            }
            MessageBox.Show("Done");
        }

        private void answer_Click(object sender, EventArgs e)
        {
            hey = false;
        }

按下answer_Click()时显示消息框。它不会结冰

您只需将问题存储在一个字段中,然后将答案逻辑放入
答案\u单击

private Question _currentQuestion;

private void AskQuestion(Question q)
{
    _currentQuestion = q.GetQuestion();
    questionbox.Text =_currentQuestion;
}

private void Answer_Click(object sender, EventArgs e)
{
   if (_currentQuestion != null)
   {
      if (_currentQuestion == answerbox.Text)
      {
          MessageBox.Show("well done");
      }

      else
      {
          MessageBox.Show("nope");
      }
   }
}

我不明白你为什么要暂停你的节目。你想在等待用户时做其他事情吗?它必须等待用户输入一个问题,然后再检查其是否正确,因此在用户将其放入文本框之前,没有必要继续下一位代码,即比较。当bool设置为false时。