Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep() problems

Status
Not open for further replies.

WelshyWizard

IS-IT--Management
Apr 23, 2006
89
GB
Hi all,

I have the following test code running on a windows form, simply to show the users a progress bar in action. As the progress bar rises, I want the label to display a different message:

Code:
        Label1.Text = "Copying Text File"
        ProgressBar1.Value += 5
        Sleep(5000)

        Label1.Text = "Establishing Database Connection"
        ProgressBar1.Value += 5
        Sleep(5000)

        Label1.Text = "Initialising Import"
        ProgressBar1.Value += 5
        Sleep(5000)

and so on...
However, although my progress bar is increasing, my label is not changing to the required text. Does anyone have any ideas as to why this would be happening?

Cheers

Today is the tomorrow you worried about yesterday - and all is well.....
 
Try this:

Code:
Imports System.Threading.Thread
Imports System.Windows.Forms.Application
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Copying Text File"
        ProgressBar1.Value += 5
        DoEvents()
        Sleep(5000)


        Label1.Text = "Establishing Database Connection"
        ProgressBar1.Value += 5
        DoEvents()
        Sleep(5000)

        Label1.Text = "Initialising Import"
        ProgressBar1.Value += 5
        Sleep(5000)
    End Sub
End Class

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Try doing a refresh after your sleep...

label1.refresh()
 
The problem is caused by the way this works under the hood. When you set the Text property of the Label it tries to update the screen by sending a windows message to the form. Because your User Interface (UI) thread is stuck in the function it never gets around to processing this message. For this reason it is better to use a Timer than have your function sleep for a long time. Or if you have a long running task you should kick off a background thread.

However there is a hack way to get around this. You can call Label1.Refresh after each time you set Label1.Text and this should cause the screen to update in the way you expect. This is not a good solution as the rest of the UI will still be hung for the duration of your function, but is probably OK for scrappy sample code.

Code:
        Label1.Text = "Copying Text File"
        Label1.Refresh()
        ProgressBar1.Value += 5
        Sleep(5000)

        Label1.Text = "Establishing Database Connection"
        Label1.Refresh()
        ProgressBar1.Value += 5
        Sleep(5000)

        Label1.Text = "Initialising Import"
        Label1.Refresh()
        ProgressBar1.Value += 5
        Sleep(5000)
 
Just a pet-peeve... but you should really think about changing the progress bar values and related messages from within the code that is processing the actual tasks, if at all possible.

Progress bars based on timers are mostly meaningless and irritate people like me. LOL!

It would also solve your problem. *wink*

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top