Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the progress bar to have certain values after specific time intervals. but the Sleep function Stops the ui from appearing.

My Code

C++
private: System::Void ProgressBarSave_Load(System::Object^  sender, System::EventArgs^  e) 
	        {
		        for (int i = 0; i < 4; i++)
			{
				Thread::Sleep(3000);
				progressBar1 -> Value +=25;	//progress Bar Value
				if ( i == 3)
				{
		                    Close();			//Closes The Dialog
				}
			}
		}
Posted

Um.
Where do I start?
If you want a progress bar to show progress, then sleeping the thread is always going to be a silly move. Firstly because it stops the UI thread from actually changing the display, and secondly, because it doesn't allow any program to actually be made unless you have that working on a different thread. And if you have it working on a different thread, then why are you reporting progress as if it wasn't?

There are a couple of possibilities: make the progress bar a Marquee in which case it will rotate itself without your intervention, or do the job properly with a BackgroundWorker and ReportProgress
 
Share this answer
 
Comments
BladeFireX 14-Nov-12 4:30am    
isn't there a delay like function in cli? if not, then how can i use ReportProgress?
and i do not have my code working on a different thread, and i have the progress bar opened up in a different dialog.
OriginalGriff 14-Nov-12 4:42am    
Why not move your code into a background worker on the dialog containing the progress bar?
Any other way is multiplying interconnections, and making your life harder.
For example, if the progress dialog is modal, then the thread that displays it is frozen until it returns - so no work can continue on that thread anyway...
Use the Timer class[^].
 
Share this answer
 

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