子例程没有从另一个线程执行

时间:2015-10-27 12:27:35

标签: vb.net

如果有人会向我解释为什么我写的代码没有在另一个线程上执行(代码只执行Button4_Click sub而没有任何异常),我将不胜感激。如果我从主线程调用timerclass()子,一切都运行完美。 我无法理解这种行为..也许我需要使用委托(?)

Private t As System.Threading.Thread
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    t = New System.Threading.Thread(AddressOf timerclass)
    t.Start()
End Sub
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)

Private Sub timerclass()
    tim = New Timer
    AddHandler tim.Tick, AddressOf tim_Tick
    tim.Interval = 500
    tim.Interval = 500
    TargetDT = DateTime.Now.Add(CountDownFrom)
    tim.Start()

End Sub

Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
    If ts.TotalMilliseconds > 0 Then
        Label2.Text = ts.ToString("mm\:ss")
    Else
        Label2.Text = "00:00"
        tim.Stop()
        MessageBox.Show("Done")
    End If

End Sub   

0 个答案:

没有答案