Question

I have a timer. This timer has to print when it starts and when it ends. The program has to wait for some time before printing the end words. What I am having that the loop runs and the time just been save for one time then print everything once the time is over. I want it to print one thing at different time.

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


        }
    }
Was it helpful?

Solution

What it looks like is that you want to use a timer to tick on a random period.

Something like this should be what you want. wire up the event in the designer if that's how you are doing it:

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top