vb.net 简单、无阻塞的睡眠方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2237567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:40:24  来源:igfitidea点击:

Simple, non-blocking way to sleep?

vb.netsleepnonblocking

提问by Gulbahar

I googled for this and read some threads here, but I haven't found a simple way to have a VB.Net application sleep for a little while and still keep the application responsive:

我在谷歌上搜索并在这里阅读了一些线程,但我还没有找到一种简单的方法来让 VB.Net 应用程序休眠一段时间并仍然保持应用程序响应:

Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Threading.Thread

[...]

''#How to keep screen frop freezing?
While True
  ListBox1.Items.Clear()

  ListBox1.Items.Add("blah")

  ''#Not much difference
  ListBox1.Refresh()

  ''#Wait 1mn
  Sleep(60000)
End While

Is there really no simple, non-blocking solution to have a VB.Net application wait for a few seconds?

真的没有简单的非阻塞解决方案让 VB.Net 应用程序等待几秒钟吗?

Thank you.

谢谢你。

回答by user408874

Public Sub ResponsiveSleep(ByRef iMilliSeconds As Integer)
    Dim i As Integer, iHalfSeconds As Integer = iMilliSeconds / 500
    For i = 1 To iHalfSeconds
        Threading.Thread.Sleep(500) : Application.DoEvents()
    Next i
End Sub

Call ResponsiveSleep to pause a particular piece of code whilst keeping the application fairly responsive. To make the application more responsive iHalfSeconds could be modified to tenths or hundredths of a second

调用 ResponsiveSleep 来暂停一段特定的代码,同时保持应用程序的响应性。为了使应用程序响应更快,可以将 iHalfSeconds 修改为十分之一或百分之一秒

回答by Seth Moore

Is this WinForms or WPF?

这是 WinForms 还是 WPF?

If it's WinForms you could just use a timer instead of a while loop. Then your app would still be responsive and raise events.

如果是 WinForms,您可以只使用计时器而不是 while 循环。然后你的应用程序仍然会响应并引发事件。

In WPF I think you would have to make your own timer based on the DispatchTimer class.

在 WPF 中,我认为您必须根据DispatchTimer 类制作自己的计时器。

回答by Gbolahan

Private Sub wait(ByVal interval As Integer)
    Dim stopW As New Stopwatch
    stopW.Start()
    Do While stopW.ElapsedMilliseconds < interval
        ' Allows your UI to remain responsive
        Application.DoEvents()
    Loop
    stopW.Stop()
End Sub

回答by Jarrod Christman

Really, the solution is two fold: 1.) Use a timer instead of sleeping, sleep does as it says, causes the thread to sleep. 2.) Use multi-threading and have your screen scrapping function run in it's own thread, this will greatly enhance the responsiveness of your application.

真的,解决方案有两个方面:1.) 使用计时器而不是睡眠,睡眠正如它所说的那样,导致线程进入睡眠状态。2.) 使用多线程并让你的屏幕抓取功能在它自己的线程中运行,这将大大提高你的应用程序的响应能力。

Threading.Thread.Sleep(IntSleepTime)

Is a thread safe sleep function that will pause the current thread for the specified time, so if you were to use sleep, you can do so in a multi-threading environment and it will keep the rest of your app responsive as you're only sleeping a branched thread, not the main thread.

是一个线程安全的 sleep 函数,它将暂停当前线程指定的时间,所以如果你要使用 sleep,你可以在多线程环境中这样做,它会让你的应用程序的其余部分保持响应,因为你只是休眠一个分支线程,而不是主线程。

回答by M9J_cfALt

How about this simple piece of code - :)

这段简单的代码怎么样 - :)

Private Async Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        MsgBox("This msgbox is shown immidiatly. click OK and hover mouse over other controls to see if UI is freezed")
        Await Task.Delay(5000)
        MsgBox("see? UI did'nt freez :). Notice the keyword 'Async' between Private and Sub")
    End Sub

Use Asyncon declared sub and then put Await Task.Delay(milliseconds)instead of Thread.Sleep(milliseconds)

Async在声明的 sub 上使用,然后 putAwait Task.Delay(milliseconds)而不是Thread.Sleep(milliseconds)

回答by Wim

The Timer suggestion is really the best way to do it. But if DoEventsstill works (I haven't done VB since 5.0), you can do this:

Timer 建议确实是最好的方法。但是如果DoEvents仍然有效(我从 5.0 开始就没有做过 VB),你可以这样做:

For i = 0 To 600
    DoEvents
    Sleep(100)
Next

This would do 600 sleeps of .1 second each, with a DoEvents between each to handle current events. The .1 second should be a good tradeoff between responsiveness (events get handled within .1 second) and CPU consumtion (do this too fast and your app will start consuming a significant amount of CPU time even while waiting).

这将执行 600 次睡眠,每次 0.1 秒,每个睡眠之间有一个 DoEvents 来处理当前事件。0.1 秒应该是响应能力(事件在 0.1 秒内得到处理)和 CPU 消耗(这样做太快,即使在等待时您的应用程序也会开始消耗大量 CPU 时间)之间的良好折衷。

回答by KrazzyRider

U are make the UI Thread Which handing the form to sleep. If u want to make ur application responsive,first make a method which add the items in list view and when ur form loads start that method using thread,now use sleep to make ur list view thread sleep,and ur from will be in responsive state..

你让 UI 线程处理表单进入睡眠状态。如果你想让你的应用程序响应,首先创建一个在列表视图中添加项目的方法,当你的表单加载时使用线程启动该方法,现在使用 sleep 使你的列表视图线程休眠,你的 from 将处于响应状态..

回答by Jimva

This is for VB not VBA

这是针对 VB 而不是 VBA

    Private Async Function PauseTime(ByVal MS As Integer) As Task

    Await Task.Run(Sub()                         
                           Thread.Sleep(MS)

                   End Sub)


     End Function