A simple, non-blocking way to sleep?

I searched this for a search and read some topics here, but I did not find an easy way to hibernate the VB.Net application for a while and still support the application:

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 a simple, non-blocking solution for the VB.Net application to wait a few seconds?

Thanks.

+7
sleep nonblocking
source share
7 answers

Is it WinForms or WPF?

If it's WinForms, you can simply use a timer instead of a while loop. Then your application will still respond and raise events.

In WPF, I think you will need to create your own timer based on the DispatchTimer class .

+7
source share
 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 

Calling ResponsiveSleep to pause a specific piece of code while maintaining the correctness of the application. To make the application more responsive, iHalfSeconds could be changed to tenths or hundredths of a second

+7
source share
 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 
+4
source share

Timer suggestions are truly the best way to do this. But if DoEvents is still working (I have not done VB since 5.0), you can do this:

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

This will do 600 dreams for .1 second each, with DoEvents between them to handle current events. 1 second should be a good compromise between responsiveness (events are processed within 0.1 second) and CPU consumption (do it too fast, and your the application will begin to consume a significant amount of processor time, even while waiting).

+2
source share

Indeed, the solution is two-fold: 1.) Use a timer instead of sleep; sleep does, as it says, makes the thread sleep. 2.) Use multithreading and you can use the screen cropping function in your stream, which will significantly increase the efficiency of your application.

 Threading.Thread.Sleep(IntSleepTime) 

It is a safe storage function in a thread that pauses the current thread for a specified time, so if you must use sleep, you can do this in a multi-threaded environment, and this will support the rest of your application, since you are a "Branched Sleeping Stream, not main stream.

+2
source share

U make a user interface thread that submits a sleep form. If you want the ur application to be receptive, first create a method that adds the elements as a list, and when ur forms loads, start this method with a stream, now use sleep to view the ur url list, and ur from will be in reaction state ..

0
source share

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
source share

All Articles