简体   繁体   English

VB.Net暂停代码

[英]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. 我一直在尝试使用在这里许多类似文章中发现的Sleep comand,但它总是使我的程序崩溃。 The last coe I tested is below: 我测试的最后一个coe如下:

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 感谢您的任何帮助,您可以提供

Don't call Sleep on the UI thread. 不要在UI线程上调用Sleep

While your code is running on the UI thread, the UI cannot update. 当您的代码在UI线程上运行时,UI无法更新。
Therefore, all of your approaches are doomed to failure. 因此,您的所有方法注定会失败。

Instead, learn about the new Async language features and write 相反,请学习新的Async语言功能并编写

Await Task.Delay(1000)

You shouldn't need to declare the kernel32 native function because Threading.Thread.Sleep() does the same thing with managed code. 您不需要声明kernel32本机函数,因为Threading.Thread.Sleep()对托管代码执行相同的操作。 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() 请参见kernel32.dll Sleep和Thread.Sleep()之间的任何区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM