-1

I have a form in which I need to give timedelay of 3 seconds before each line executes. But when I try system.threading.thread.sleep it's freezing the UI and furthermore all delays are executing as one single delay. that is

if I have

label.text = "intializing..."
sleep(5000)
label1.text = "connectinh..."
sleep(5000)
label2.text = "connected..."
sleep(5000)

the UI freezes for 15 seconds totally and then all executiong happens instantaneously as if there was no delay statement between them. I was not able to find out anything else other than threading.sleep.

2
  • 2
    Saying "this is urgent" won't get you quicker answers. At best, people will just ignore it; at worst, it will irritate them and they'll stop reading... Apr 9, 2011 at 1:35
  • its just an adjective, i dont know why ppl have to get irritated with it. I explained my problem clearly and said its urgent. I dont think theres nothing wrong with it.
    – swordfish
    Apr 10, 2011 at 4:29

5 Answers 5

2

What Ron said:

Private Sub Form1_Shown(sender As Object, _
                        e As System.EventArgs) Handles Me.Shown
    Dim t As New Threading.Thread(AddressOf showLabelText)
    t.IsBackground = True
    t.Start()
End Sub

Const delaySecs As Integer = 3

Private Sub showLabelText()
    Dim del As New foo(AddressOf updLabel)

    Label1.BeginInvoke(del, "one")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "two")
    Threading.Thread.Sleep(delaySecs * 1000)

    Label1.BeginInvoke(del, "three")
    Threading.Thread.Sleep(delaySecs * 1000)

End Sub

Delegate Sub foo(theText As String)

Private Sub updLabel(theText As Object)
    Label1.Text = DirectCast(theText, String)
End Sub
1
  • Actually this is what i have done, in a slightly modified way
    – swordfish
    Apr 10, 2011 at 4:31
1

an inline solution, no multi-threading, no timers.

    Sub delay(ByVal delay_ms As Integer)
        Dim tspan As New TimeSpan
        Dim tstart = Now
        While tspan.TotalMilliseconds < delay_ms
            tspan = Now - tstart
            Application.DoEvents()
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Hello"
        delay(100) 'leaks 572 ns every 10 ms
        Label1.Text = "Salut"
    End Sub
0

You need to have each label update in a separate event and let it refresh the UI. Create a timer that will call your event handler and in it update a different label every time

0

Try the following:

label.text = "intializing..."
Application.DoEvents
sleep(5000)
label1.text = "connectinh..."
Application.DoEvents
sleep(5000)
label2.text = "connected..."
Application.DoEvents
sleep(5000)

Application.DoEvents tells the OS process underlying work, such as updating the text value.

0
label.text = "intializing..."
label.Refresh
sleep(5000)
label1.text = "connectinh..."
label1.Refresh
sleep(5000)
label2.text = "connected..."
label2.Refresh
sleep(5000)

I think this should work more properly than DoEvents!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.