Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed Execution In VB.NET

Tags:

vb.net

wpf

timer

I am in need to delay the execution of a line of code, say, by 5 seconds. But by doing

System.Threading.Thread.Sleep(5000)

the whole UI hangs and I don't want that to happen. Because I want to be able to display something in a Window that a user will be able to see. Does anyone know of an alternative of achieving this?

Thanks!

like image 382
will0809 Avatar asked Sep 05 '26 18:09

will0809


1 Answers

Use could use an Timer that executes once after 5 seconds. In WPF you will normally use a DispatcherTimer.

dispatcherTimer = New Threading.DispatcherTimer()
AddHandler dispatcherTimer.Tick, AddressOf dispatcherTimer_Tick
dispatcherTimer.Interval = New TimeSpan(0,0,5)
dispatcherTimer.Start()

Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
  dispatcherTimer.Stop();
  '' Your code to be executed after 5 seconds
End Sub
like image 194
Jehof Avatar answered Sep 08 '26 05:09

Jehof



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!