So this is a small part of a bigger project I'm working on. I'm having trouble with having a visual countdown starting from a value the user inputs. I have three elements on the form textBox1 where the user enters the value to start from and label1 where the visual countdown should take place. The button1 will start the countdown. When I run the program and click on the button the countdown will start and finish correctly by displaying Done in the textBox1 and 0 in label1 but during the countdown label1 did not show 9..8..7...6..5...4...3....2..1..0.
I also tried my program in win32 Console Application with cout and it worked perfectly fine. Can some one explain to me why it will not work in a GUI.
Here is my code for the button1 and the function wait(), I'm also using #include <time.h>

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Int32 i=Convert::ToInt32(textBox1->Text);					 
				 for (Int32 n=i; n>0; n--)
				 {
					 i--;
					 wait (1);
					 label1->Text=i.ToString();
				 }
				 if(i<=0)
				 {
					textBox1->Text="Done";
				 }		
			 }

Is that a MS-Windows GUI program? If it is, then you need to add a message pump in the loop so that Windows has a chance to update the window. I would put it inside that wait function.

Is that a MS-Windows GUI program? If it is, then you need to add a message pump in the loop so that Windows has a chance to update the window. I would put it inside that wait function.

I'm not sure what message pump is. That is above the level of programing that I'm at. Could you explain it to me. Thanks
I looked it up on the web and found this:

MSG msg;
    while(GetMessage(&msg, hwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

     

        // do stuff

    }

Yes, that's called a message pump. So your wait() function could be this:

void DoMessagePump()
{
    MSG msg;
    while(GetMessage(&msg, hwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) 
  {
     DoMessagePump();
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.