Label output BEFORE Sleep() pauses

I'm trying to figure out how to use Sleep to pause the program for a few seconds before making label2 invisible again. What ends up happening is the system sleeps for 5 seconds and then performs all of the output so the end result is nothing.

Here's the offending chunk of code (Windows Forms in VS2008), I'll post the entire thing for you if necessary. The way it's written it should sleep for 5 seconds before resetting everything basically, but I guess the label text is being held in a buffer or something while it's sleeping.


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int sort = getHouse();
srand(sort);
sort = rand() % 5;

switch (sort){
case 1: this->label2->ForeColor.Red;
this->label2->Text = "Gryffindor!";
this->label2->Visible = true;
break;
case 2: this->label2->ForeColor.Green;
this->label2->Text = "Slytherin!";
this->label2->Visible = true;
break;
case 3: this->label2->ForeColor.Yellow;
this->label2->Text = "Hufflepuff!";
this->label2->Visible = true;
break;
case 4: this->label2->ForeColor.Blue;
this->label2->Text = "Ravenclaw!";
this->label2->Visible = true;
break;
default: this->label2->ForeColor.Black;
this->label2->Text = "Sorry, You are a Muggle!";
this->label2->Visible = true;
break;
}
this->textBox1->Text = "";
this->textBox1->Focus();
Sleep(5000);
this->label2->Visible = false;
}





You probably just need to call Refresh() on the label or textbox before calling Sleep().
That's exactly what I was looking for! Thanks a ton.
I'm not sure it's a good idea to block a GUI thread by calling Sleep(). Esp. for 5 seconds.

Normally you would set up a one shot timer to trigger a (in your case) reset event in 5 secs time.

As your using Windows Forms, check out the Timer class.

Andy
Last edited on
Topic archived. No new replies allowed.