Click here to Skip to main content
15,885,883 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone!

My question is related to Window Form Application and System::Threading::Thread::Sleep(int delayTime) in Visual C++.

My window form has a panel, in which there are 3 ovalShape named ovalShape1, ovalShape2 and ovalShape3. A button name button is next to the panel. Button is associated to a click event. What I need is when I click the button, the color of every oval shape will be changed to red after every 1 second (1000 miliseconds).
This is what I’ve done:

private: System::Void buttonClick(System::Object^  sender, System::EventArgs^  e) {
			  
	this->ovalShape1->FillColor = System::Drawing::Color::Red;
	System::Threading::Thread::Sleep(1000);
	this->ovalShape1->FillColor = System::Drawing::Color::White;

			
	this->ovalShape2->FillColor = System::Drawing::Color::Red;
	System::Threading::Thread::Sleep(1000);
	this->ovalShape2->FillColor = System::Drawing::Color::White;

	this->ovalShape3->FillColor = System::Drawing::Color::Red;
	System::Threading::Thread::Sleep(1000);
	this->ovalShape3->FillColor = System::Drawing::Color::White;

}


But for some reason (I don’t know), the application doesn’t work the way I have expected. The ovalShape1 just flashes and that’s all.

Maybe you know the reason!

Any help or suggestion would be appreciated!

[enhzflep: CLI tag added to question]
Posted
Updated 30-May-12 0:54am
v4

The Sleep will just suspend/stop everything; nothing will happen; not even a redraw.

You could use a timer instead (SetTimer) to change the color, or use a thread (overkill IMO).


I'm at home right now (no compiler, yeah, a developer with no compiler at home!); so I can't try it out.

Max.
 
Share this answer
 
Comments
svatstika 30-May-12 21:45pm    
Well, thank you very much for the reply. I have one more question. What should I do if I just want some Timer working only after I clicked on a button?
svatstika 30-May-12 21:58pm    
Hi Maximilien, I think I've found the answer for the question above. That is timer->Enabled == true or false.
 
Share this answer
 
Comments
Nelek 29-Apr-14 17:35pm    
Carlo, you got a littel bit late for this question :P
CPallini 30-Apr-14 3:34am    
:-D
Who knows? Possibly the OP is still waiting for a solution. Shame on me!
Nelek 30-Apr-14 12:25pm    
Don't be so hard with you. You just got fooled by the question being "awaken" with the other solution.
There is always the positive side: It can help other people having similar problem
I had a very similar that I found could be solved using Application::DoEvents() before the sleep function. This way, the command to color in the form will be processed before the sleep function:

this->ovalShape1->FillColor = System::Drawing::Color::Red;
Application::DoEvents();
Sleep(1000);
this->ovalShape1->FillColor = System::Drawing::Color::White;
 
Share this answer
 
Comments
[no name] 29-Apr-14 14:52pm    
The use of DoEvents is almost always the major red flag of a bad design.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900