在表单中,有一个标签。它显示每分钟是否连接Web服务。如何编码重复此过程?我会使用线程还是计时器?请与我分享。
答案 0 :(得分:1)
您需要一个计时器对象才能每隔X分钟运行一次代码。如果需要一段时间检查并且您希望表单在此期间保持响应,则只需要使用单独的线程来检查Web服务。
使用计时器非常简单:
Private WithEvents timer As New System.Timers.Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set interval to 1 minute
timer.Interval = 60000
'Synchronize with current form, or else an error will occur when trying to
'update the UI from within the elapsed event
timer.SynchronizingObject = Me
End Sub
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer.Elapsed
'Add code here to check if the web service is updated and update label
End Sub