Question

I have created dynamic timer. That takes different time intervals. But all the information are appearing one time. I want each piece of information will appear just after its time is over.

How can I do that?

   private void btnTest_Click(object sender, EventArgs e)
    {
        for (int x = 0; x <= 5; x++)
        {
            System.Timers.Timer timer = new System.Timers.Timer();

            timer.AutoReset=true;

            timer.Start();
            timer.Interval = ((x + 1) * 100);
            this.txtOutput.Text += "\r\r\n" + "  this out put equal to " + ((x + 1) * 100);
            Thread.Sleep((x + 1) * 100);

           this.txtOutput.Text += "\r\r\n" + "  Ends x " + x;



        }
    }
Was it helpful?

Solution

I think you are using the wrong timer. Try using the System.Windows.Forms.Timer instead and use the actual tick event to perform your logic:

var timer = new System.Windows.Forms.Timer();
int track = 0;
timer.Tick += (timerObject, timerArgs) => {
  timer.Interval = ((track + 1) * 100);
  this.txtOutput.Text += "\r\r\n" + " this out put equal to "
                                  + ((track + 1) * 100);
  ++track;
  if (track > 4) {
    timer.Stop();
    timer.Dispose();
    this.txtOutput.Text += "\r\r\n" + "  Ends x " + track.ToString();
  }
};
timer.Start();

OTHER TIPS

First of all, you have to do this outside the UI thread, the text are showed after the return off btnTest_Click method, and while the Thread.Sleep((x + 1) * 100); is running, the UI thread is frozen.

One way to do this is using the BackgroundWorker, it will run the code inside the DoWork event in another thread.

But, you have to be careful, UI controls can be only accessed from UI thread, so you have to use the BeginInvoke method of the Form. More information in this thread.

Using the BackgroundWorker:

private void btnTest_Click(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;

        bw.RunWorkerAsync();
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int x = 0; x <= 5; x++)
        {
            System.Timers.Timer timer = new System.Timers.Timer();

            timer.AutoReset = true;

            timer.Start();
            timer.Interval = ((x + 1) * 100);

            this.BeginInvoke(new MethodInvoker(delegate
            {
                this.txtOutput.Text += "\r\r\n" + "  this out put equal to " + ((x + 1) * 100);
            }));

            Thread.Sleep((x + 1) * 100);

            this.BeginInvoke(new MethodInvoker(delegate
            {
                this.txtOutput.Text += "\r\r\n" + "  Ends x " + x;
            }));
        }
    }

In Windows Forms you can do so

for (var x = 0; x <= 5; x++)
{
    textBox1.Text += "\r\r\n" + "  this out put equal to " + ((x + 1) * 100);
    textBox1.Text += "\r\r\n" + "  Ends x " + x;
    Application.DoEvents();
    Thread.Sleep(200);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top