Hi everyone.

I am creating a small windows form application using Visual C++; and I would need to delay a loop by a second. Sleep() doesn't work.(Not sure why) Is there a way I can use the timer? Any suggestions on how can I do that?

Thanks in advance. :)

8 Years
Discussion Span
Last Post by hajiakhundov

How do you know Sleep() doesn't work?? Recall the parameter is milliseconds -- there are 1000 milliseconds in one second.

Yeah, I know. It is still not working. Well, it does work when I do a console application, but not when I do it in the Forms app.

The following does not work:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

				 for (int k = 0; k < 10; k++)
					  label1->Text = Convert::ToString (k);

Edited by hajiakhundov: n/a


What do you mean by "it doesn't work"?

Well, it should change the Text of label1 from 1 to 9, with a 1 second interval. However it does not, it shows me, a 9 right away, when I click the button.


It looks like you're using managed C++, so try using:

using namespace System::Threading;

I'm surprised that you find plain old Sleep() available, so perhaps you're picking up an odd version of Sleep from somewhere in your codebase.

Edited by MrSpigot: Got smiley because I didn't use code tags.


MrSpigot, this is not working either :(

using namespace System::Threading;

why don't you make your own function using <ctime>? Just let it loop for a second every time.


why don't you make your own function using <ctime>? Just let it loop for a second every time.

Yeah, Tigran, that was a good idea.
I did something like

Time b,c; // class Time,  Constructs a Time object that is set to 
  //    the time at which the constructor executes.

// the rest is pretty clear.

int t1 = b.get_seconds(); // seconds at that time. when b was created;
int t2;
 while (true)
         Time c;  t2 = c.get_seconds(); if (t2==t1+5) break; // creates //new c each time, (+5) indicates 5 seconds delay. 

But anyways, I would like to know how Timer works in Visual c++.

Edited by hajiakhundov: forgot to [code]

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.