让计时器每2秒完成一次不同的任务C#

时间:2018-09-26 22:50:23

标签: c#

我想让计时器每2秒说一次不同的行,而不是一次将所有文本添加到文本框中,我想给用户机会阅读对话框,而不是将其全部添加到对话框中。盒子马上。这是我到目前为止的代码,但似乎没有输出文本。

private void OaksSpeakingTimer_Tick(object sender, EventArgs e)
    {

        //Starting Oaks basic speaking timer
        OaksSpeakingTimer.Start();
        ProfessorTxt.Text += "LLLLLLL";
        OaksSpeakingTimer.Stop();
        OaksSpeakingTimer.Start();
        ProfessorTxt.Text += "\rrrrrrrrrrrrrrrrrrrrrrrrrrr.";
        OaksSpeakingTimer.Stop();
        OaksSpeakingTimer.Start();
        ProfessorTxt.Text += "\nttttttttttttttttttttttttttttttttttttttttt!";
        OaksSpeakingTimer.Stop();
    }

1 个答案:

答案 0 :(得分:2)

您可以将所有预期的文本放在Queue<string>中,并在计时器触发时使下一个字符串出队:

private Queue<string> messages = new Queue<string> 
{
    "LLLLLLL",
    "\rrrrrrrrrrrrrrrrrrrrrrrrrrr.",
    "\nttttttttttttttttttttttttttttttttttttttttt!"
};

private void OaksSpeakingTimer_Tick(object sender, EventArgs e)
{
    if (messages.Count == 0) 
    {
        OaksSpeakingTimer.Stop();
        return;
    }

    var message = messages.Dequeue();
    ProfessorTxt.Text += message;
}