Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除所有计时器VB.net_Vb.net_Timer - Fatal编程技术网

删除所有计时器VB.net

删除所有计时器VB.net,vb.net,timer,Vb.net,Timer,我在类中创建计时器 Dim timer As New Timer timer.Enabled = True timer.Interval = 1000 timer.Tag = "TimeslipTimer_" & timeslip.id AddHandler timer.Tick, AddressOf GlobalTimerTick

我在类中创建计时器

Dim timer As New Timer
                timer.Enabled = True
                timer.Interval = 1000
                timer.Tag = "TimeslipTimer_" & timeslip.id
                AddHandler timer.Tick, AddressOf GlobalTimerTick
                timer.Start()
我遇到的问题是,如果需要,如何删除这些内容

当时,我想在列表中添加计时器,如下所示,但我没有成功

dim timers as new list(of Timer)

For Each c As Timer In Timers
                c.Dispose()
                Timers.Remove(c)
            Next

从列表中删除时,必须按相反顺序访问列表。还要记住删除处理程序

Public Class Form1

    Private WithEvents timer As Timer
    Private timers As New List(Of Timer)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        timer = New Timer
        timers.Add(timer)
        timer.Enabled = True
        timer.Interval = 1000
        'timer.Tag = "TimeslipTimer_" & timeslip.id
        AddHandler timer.Tick, AddressOf GlobalTimerTick
        timer.Start()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For x As Integer = timers.Count - 1 To 0 Step -1
            Dim t As Timer = timers(x)
            RemoveHandler t.Tick, AddressOf GlobalTimerTick
            t.Stop()
            t.Dispose()
            timers.RemoveAt(x)
        Next
    End Sub

    Private Sub GlobalTimerTick(sender As Object, e As EventArgs)
        Debug.WriteLine("TICK")
    End Sub
End Class
使用Linq Timers.ForEachSubt RemoveHandler t.勾选,GlobalTimerTick的地址:t.处置结束子:计时器。清除此选项,确保ypu已导入系统。Linq