for循环的计时器

本文关键字:计时器 循环 for | 更新日期: 2023-09-27 18:03:06

我有一个计时器。这个计时器必须打印何时开始和何时结束。程序必须等待一段时间才能打印结束单词。我所拥有的是,循环运行,时间被保存一次,然后在时间结束后打印所有内容。我想让它在不同的时间打印一件东西。

    //Intializing the variables
    Random valueTimer = new Random();
     Queue timegetvalue = new Queue();
    int [] timeinter = new int [20];
    int value;
    int savenum;
          //the button to run the timer
         private void btnRun_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= 10; i++)
        { 
           //this to get random time for waiting
            value = valueTimer.Next(10, 100);
            savenum = value;
            timer1.Enabled = true;
                timer1.Start();
                txtOutput.Text += "'r'r'n" + i + " Starts:  " + savenum;
                Thread.Sleep(savenum);
                txtOutput.Text += "'r'r'n" + i + " Ends:  " + savenum;

        }
    }

for循环的计时器

它看起来就像你想要使用一个计时器在一个随机的周期上打号。

像这样的东西应该是你想要的。在设计器中连接事件(如果你是这么做的):
private void timer_Tick(object sender, EventArgs e)
{
     var timer = (Timer)sender;
     timer.Stop();
     //you should check how many time's you have fired and decide whether to keep going here.

     timer.Interval = valueTimer.Next(10, 100);
     timer.Start();
}

private void btnRun_Click(object sender, EventArgs e)
{
    timer.Interval = valueTimer.Next(10, 100);
    timer1.Start();
}