will0809
will0809

Reputation: 237

Delayed Execution In VB.NET

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!

Upvotes: 3

Views: 2124

Answers (1)

Jehof
Jehof

Reputation: 35554

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

Upvotes: 5

Related Questions