Question Problem with waiting in loop (threading.thread.sleep not work as normal)

maxforummfv

New member
Joined
Jan 2, 2012
Messages
3
Location
Vietnam
Programming Experience
Beginner
Hi everybody!
I'm a new member here and ofcourse, I'm a newbie of VB.NET, too.

Well, I am not an English or American..., so I am not good at English, sorry about that.
But I really need help in this situation. I created an VB application by Visual Studio 2008. It called "Random Number".

I want to make that application because I have a girl shoes shop, so I want to make a program show Random product with its price in my computer. That "random number" application is my small first test before I can do that program.

Let's begin my problem, hihi.

Well, this is my application graphic:


random1.jpg


As you can see, my application has 4 main parts:
- Lower number (txtLower): This is where I put the Lowest number.
- Higher number (txtHigher): This is where I put the Highest number.
- Button Random Begin (btRandom): button to activate the program.
- Lable Ramdom (lbRandom): this is where the "random number" between Lower and Higher number show up.

I want an application where I can put Lowest and Highest number and then the program will show the random number between them automatically.

So, I make I loop test like this code:
VB.NET:
Private Sub btRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRandom.Click


        Dim lower, higher, i As Integer
        Dim R As New Random
        lower = txtLower.Text
        higher = txtHigher.Text


        For i = 1 To 10
            lbRandom.Text = R.Next(lower, higher)
            Threading.Thread.Sleep(500)
        Next


End Sub

So, as my test code, it means after each 0,5 second, one random number will show up.
But, the problem is it was not. In fact, It wait for the loop end, it show. Means after 5 seconds (cause i use loop from 1 to 10, each time 500 ms), just 1 random number show.
It is not what I want.

So, if you have time, can you please help me to know how to solve that problem.
Thank you so much!
 

Attachments

  • random2.jpg
    random2.jpg
    31.8 KB · Views: 29
Oh, anyways, thank you. I find a way to solve problem myself.
Just put Application.DoEvents() before threading.thread.sleep, cause the UI thread is not getting a chance to actually update the label before it goes to sleep, hihi

 
Back
Top