简体   繁体   English

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

[英]Having A Timer Complete A Different Task Every 2 Seconds C#

I'm trying to have the timer say a different line every 2 seconds instead of adding all of the text into the text box at once, I wanna give the user the opportunity to read the dialog instead of it all being added into the box at once. 我试图让计时器每2秒说一次不同的行,而不是立即将所有文本添加到文本框中,我想让用户有机会阅读对话框,而不是将对话框全部添加到对话框中一旦。 This is the code I have so far but it doesn't seem to output the text. 这是我到目前为止的代码,但似乎没有输出文本。

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();
    }

You could put all the intended text in a Queue<string> and dequeue the next string whenever the timer fires: 您可以将所有预期的文本放在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM