Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让计时器每2秒完成一项不同的任务C#_C# - Fatal编程技术网

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

让计时器每2秒完成一项不同的任务C#,c#,C#,我试图让计时器每2秒说一行不同的话,而不是一次将所有文本添加到文本框中,我想让用户有机会阅读对话框,而不是一次将所有文本添加到文本框中。这是我到目前为止的代码,但它似乎没有输出文本 private void OaksSpeakingTimer_Tick(object sender, EventArgs e) { //Starting Oaks basic speaking timer OaksSpeakingTimer.Start();

我试图让计时器每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();
    }

每当计时器触发时,您可以将所有预期文本放入一个字符串中,并将下一个字符串出列:

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;
}
专用队列消息=新队列
{
“llllllll”,
“\rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr。”,
“\nttttttttttttttttttttttttttttttttttttttttttt!”
};
私有void OAKSpeakingTimer_Tick(对象发送方,事件参数e)
{
如果(messages.Count==0)
{
OaksSpeakingTimer.Stop();
回来
}
var message=messages.Dequeue();
ProfessorTxt.Text+=消息;
}

计时器的工作方式与您预期的略有不同。您使用它就像“睡眠”,但实际上,它们在时间流逝时调用委托。检查示例。我看到上面的代码只显示最后一行。。使用
if
检查当前文本并更改为下一个模式如何?您是指使用if语句检查是否键入了第一行?如果(line typed=true),则键入下一行。是的,这里的第一个问题是你必须了解计时器是如何工作的,当你这样做时,你必须知道如何创建一个队列或一个要说的事情的列表,并在每次遇到timmer事件时对该队列/列表进行写入/弹出