VB.NET中的延迟执行

wil*_*809 3 vb.net wpf timer

我需要延迟执行一行代码,比如5秒钟.但通过做

System.Threading.Thread.Sleep(5000)

整个UI挂起,我不希望这种情况发生.因为我希望能够在用户将能够看到的窗口中显示某些内容.有没有人知道实现这一目标的替代方案?

谢谢!

Jeh*_*hof 5

使用可以使用在5秒后执行一次的Timer.在WPF中,您通常会使用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
Run Code Online (Sandbox Code Playgroud)