开发者

Simple, non-blocking way to sleep?

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:

Imports System.Net
Impor开发者_Go百科ts 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?

Thank you.


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


Is this WinForms or 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.

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


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


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 Async on declared sub and then put Await Task.Delay(milliseconds) instead of Thread.Sleep(milliseconds)


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.

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.


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

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).


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..


This is for VB not VBA

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

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

                   End Sub)


     End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜