user2452969
user2452969

Reputation: 1

VB.Net Pause Code

I am currently trying to make a simple slot machine app. I am trying to make it like a traditional slot machine with the spinner where it goes through and shows the different pictures. I have been trying to use a Sleep comand found in many similar posts on here but it keeps crashing my program. The last coe I tested is below:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = CStr(Int(Rnd() * 10))  'pick numbers
        Label2.Text = CStr(Int(Rnd() * 10))
        Label3.Text = CStr(Int(Rnd() * 10))
    Threading.Thread.Sleep(1000)

    Label1.Text = CStr(Int(Rnd() * 10))  'pick numbers
    Label2.Text = CStr(Int(Rnd() * 10))
    Label3.Text = CStr(Int(Rnd() * 10))
    Threading.Thread.Sleep(1000)

    Label1.Text = CStr(Int(Rnd() * 10))  'pick numbers
    Label2.Text = CStr(Int(Rnd() * 10))
    Label3.Text = CStr(Int(Rnd() * 10))
    Threading.Thread.Sleep(1000)
End Sub

Can anyone make a recommendation of what I need to change? I want it to pop up a few different numbers and switch between them each second. Thanks for any help you can provide

Upvotes: 0

Views: 4779

Answers (2)

Nick Bauer
Nick Bauer

Reputation: 1072

You shouldn't need to declare the kernel32 native function because Threading.Thread.Sleep() does the same thing with managed code. The latter is preferred, and in fact what you are already using in the event handler.

See Any difference between kernel32.dll Sleep and Thread.Sleep()

Upvotes: 0

SLaks
SLaks

Reputation: 887275

Don't call Sleep on the UI thread.

While your code is running on the UI thread, the UI cannot update.
Therefore, all of your approaches are doomed to failure.

Instead, learn about the new Async language features and write

Await Task.Delay(1000)

Upvotes: 1

Related Questions