for循环的定时器

时间:2014-05-13 05:52:31

标签: c# winforms

我有一个计时器。此计时器必须在启动时和结束时打印。在打印结束词之前,程序必须等待一段时间。我所拥有的循环运行和时间刚刚保存一次然后打印一切时间结束。我希望它能在不同的时间打印一件东西。

    //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;


        }
    }

1 个答案:

答案 0 :(得分:0)

它看起来像是你想用一个计时器来随机打勾。

这样的事情应该是你想要的。如果您正在做这件事,请在设计师中结束活动:

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