Change the text of a label in a loop

borris83

Member
Joined
Apr 30, 2009
Messages
23
Programming Experience
Beginner
I am using a for each loop and each time the loop executes, the text of the label has to be changed.. But for some reason, it only changes the text of the label during the last execution of the loop...

Here is the code:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim dll
        Dim myarray() As String = {"jscript.dll", "vbscript.dll", "activeds.dll", "audiodev.dll", "browseui.dll", "browsewm.dll"}
        For Each dll In myarray
            Process.Start("regsvr32", "/s " & dll)
            Label1.Text = "registering " & dll
            System.Threading.Thread.Sleep(1000)
        Next

    End Sub

Pls help
 
Last edited:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim dll
        Dim myarray() As String = {"jscript.dll", "vbscript.dll", "activeds.dll", "audiodev.dll", "browseui.dll", "browsewm.dll"}
        For Each dll In myarray
            Process.Start("regsvr32", "/s " & dll)
            Label1.Text = "registering " & dll
            Application.DoEvents()
            System.Threading.Thread.Sleep(1000)
        Next

    End Sub

Not the best way to do it but it will work.
 
The Application.DoEvents() should be enough, you can remove the Sleep. The DoEvents will allow the interface to update which each loop althouth this slows the execution speed of the loop itself with each iteration.
 
How about using the WaitForExit on the process instead of having it sleep.
VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim prc As Process
        Dim myarray() As String = {"jscript.dll", "vbscript.dll", "activeds.dll", "audiodev.dll", "browseui.dll", "browsewm.dll"}
        For Each dll As String In myarray
            prc = Process.Start("regsvr32", "/s " & dll)
            Label1.Text = "registering " & dll
            Application.DoEvents()
            prc.WaitForExit()
        Next

    End Sub
 
Back
Top