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
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?
Thank you.
Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.
You could also do throws InterruptedException in method definition to avoid try/catch block , or use TimeUnit. MILLISECONDS. sleep() instead.
Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context. There is no need to call sleep() from Synchronized context.
Yes, sleep is blocking.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With