|
-
Dec 3rd, 2003, 12:26 PM
#1
Thread Starter
Junior Member
Small problem with label or textbox updates
I have a small, yet annoying problem. When I perform a "FOR" or "DO", I want to update a label or textbox as the loop progresses, for example: if I want to display a percentage done while incrementing a ProgressBar. The problem is that the label or textbox will never update, it wont even clear when I restart the loop!
simplified code example:
The label clear and = i never happen, only the "Done!"
Code:
ProgressBar1.Value = 0
ProgressBar1.Maximum = 150
Label1.Text = " "
For i = 0 To 150
ProgressBar1.Increment(2)
i += 1
System.Threading.Thread.Sleep(5)
Label1.Text = i
Next
Label1.Text = "Done!"
-
Dec 3rd, 2003, 12:44 PM
#2
That is because you are sleeping the thread or otherwise processing which doesn't give the UI time to actually show the changes. Just add an Application.DoEvents right after you change the text in the loop and it should show then.
-
Dec 3rd, 2003, 12:47 PM
#3
Addicted Member
VB Code:
ProgressBar1.Value = 0
ProgressBar1.Maximum = 150
Label1.Text = " "
For i = 0 To 150
ProgressBar1.Increment(2)
i += 1
System.Threading.Thread.Sleep(5)
Label1.Text = i
[B]Application.DoEvents()[/B]
Next
Label1.Text = "Done!"
Who needs rhetorical questions anyway?
Bazza NET - The place you want to be!

-
Dec 3rd, 2003, 12:47 PM
#4
Thread Starter
Junior Member
Originally posted by Edneeis
That is because you are sleeping the thread or otherwise processing which doesn't give the UI time to actually show the changes. Just add an Application.DoEvents right after you change the text in the loop and it should show then.
Thanks!
-
Dec 3rd, 2003, 01:10 PM
#5
Frenzied Member
does that take care of refreshing the control as well? I did one of these but all I used were longer loop times and a refresh statement. I heard that the application.doevents() isn't really desirable in any situation. dont know why.
-
Dec 3rd, 2003, 01:43 PM
#6
What Application.DoEvents does is allow/force all of the threads in a process (possibly other processes as well) to work or update. This is bad in that if you are looping and there is no need for it then it makes the looping thread slower since it spends time waiting for other threads. This is good because sometimes you wait other threads to update, like the UIThread, even though it will eat clock cycles.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|