private void button1_Click(object sender, EventArgs e) { timer1.Start(); listBox1.Items.Add("First Item"); for (int i = 0; i < 1000;i++ ) toolStripProgressBar1.Value += 1; Application.DoEvents(); Thread.Sleep(5000); listBox1.Items.Add("Second Item"); } private void timer1_Tick(object sender, EventArgs e) { toolStripStatusLabel1.Text = "Please, wait..."; //через некоторое время таймер остановится, код опустил } 

In fact, I just want to simulate a slight delay in the application. and before adding the 1st element to the listbox, I start a timer in which a simple message is displayed, ala "wait a couple of seconds", and so it either starts only after the application has slept for 5 seconds, it starts up, but it does has time.

  • Those. Do you want to add the first entry, output "Please, wait", wait five seconds and add the second entry? - andreycha
  • It is, well, still while these same 5 seconds go, the progress bar worked, in accordance with the timer ticks gradually increased. In fact, I simply imitate the delay, saying that the application works with big data. - Pyrejkee
  • one
    Since the answer has already been given, remember that Thread.Sleep (); sends a stream to sleep with your application, and has nothing to do with the timer you created. - Edick Bily

1 answer 1

First of all, you should carefully read how the timer works, because now, judging by the code, you do not have this understanding.

The code that should output "Please wait" before starting the timer, you have for some reason in timer1_Tick() , and the code that should be in a tick is located in button1_Click() . Delay is not needed at all, since you already have a timer!

The scheme should be as follows:

  • add the first entry
  • print "Please wait"
  • we start the timer
  • in each tick we increase the progress bar value
  • as soon as we’ve finished counting, stop the timer, add the second entry, and remove "Please wait"

It should be like this:

 private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add("First Item"); toolStripStatusLabel1.Text = "Please, wait..."; progressBar1.Value = 0; // за 5 секунд тикнет 100 раз, этого достаточно, можно даже и реже timer1.Interval = 50; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { toolStripProgressBar1.Value++; // подразумевается, что максимум равен 100 // в любом случае интервал таймера * максимум прогресс бара // должен давать желаемую задержку в мс if (toolStripProgressBar1.Value == 100) { timer1.Stop(); toolStripStatusLabel1.Text = string.Empty; listBox1.Items.Add("Second Item"); } } 

Alternatively, you can have two timers to get rid of the unobvious dependency "timer interval * maximum progress bar == delay in ms." The first timer ticks every 100 ms and increases the progress bar. The second timer is activated after 5 seconds, stops both timers and adds / updates records.

  • one
    This is how the logic of building an application flushes out extra questions, thank you! - Pyrejkee
  • one
    @ KirillKiryanchikov logic discards everything except the dictates of the heart :). It's my pleasure! - andreycha