VB.NET Timer.Start()循环无任何值

VB.NET Timer.Start()循环无任何值,vb.net,Vb.net,我正在尝试循环所有计时器并停止所有计时器,但我正在运行的计时器除外 代码 'Block timers Private Sub blocktimers() Dim i As Integer For Each c As Timer In Me.components.Components If TypeOf c Is Timer Then If c.Enabled And c.Interval <> 100 Then

我正在尝试循环所有计时器并停止所有计时器,但我正在运行的计时器除外

代码

'Block timers
Private Sub blocktimers()
    Dim i As Integer
    For Each c As Timer In Me.components.Components
        If TypeOf c Is Timer Then
            If c.Enabled And c.Interval <> 100 Then
                carray(i) = c
                ReDim Preserve carray(i + 1)
                c.Stop()
            End If
        End If
    Next
End Sub

'Release timers
Private Sub releasetimers()
    For Each c As Timer In carray
        If c IsNot Nothing Then
            c.Start()
        End If
    Next
End Sub
“块定时器
专用子块计时器()
作为整数的Dim i
对于Me.components.components中的每个c作为计时器
如果c的类型是计时器,那么
如果c.已启用且c.间隔为100,则
卡雷(i)=c
雷迪姆保留卡雷(i+1)
c、 停止()
如果结束
如果结束
下一个
端接头
“释放计时器
专用子释放计时器()
对于carray中的每个c作为计时器
如果c不是零,那么
c、 开始()
如果结束
下一个
端接头
blocktimers()循环所有计时器,但releasetimers()仅循环2个计时器,第2个计时器值为:Nothing


如果blocktimers()循环例如10个计时器,则releasetimers()仅循环1个。

在向数组添加计时器后,您忘记了递增
i

carray(i) = c
ReDim Preserve carray(i + 1)
c.Stop()

i += 1 '<-- You forgot this.
这假设每个组件都是一个计时器,这意味着如果您使用任何不是计时器的组件,代码将中断

改为:

For Each c As Object In Me.components.Components
建议:

由于您使用的是动态大小的数组,因此我建议您切换到a以避免一些麻烦:

'Our list of timers.
Dim Timers As New List(Of Timer)

Private Sub blocktimers()
    'Clear the list before we start adding timers (just in case you decide to call this method multiple times).
    Timers.Clear()

    For Each c As Object In Me.components.Components
        If TypeOf c Is Timer Then
            If c.Enabled AndAlso c.Interval <> 100 Then
                'Cast 'c' to a Timer and add it to the list.
                Timers.Add(DirectCast(c, Timer))
                c.Stop()
            End If
        End If
    Next
End Sub

Private Sub releasetimers()
    'Iterate our Timers list.
    For Each c As Timer In Timers
        'While possible, you can be fairly certain at this point that a timer is not null.
        c.Start()
    Next
End Sub
”我们的计时器列表。
变暗定时器作为新列表(定时器)
专用子块计时器()
'在开始添加计时器之前清除列表(以防您决定多次调用此方法)。
计时器清除()
对于Me.components.components中的每个c作为对象
如果c的类型是计时器,那么
如果c.已启用且c.间隔为100,则
将“c”强制转换为计时器并将其添加到列表中。
定时器。添加(DirectCast(c,定时器))
c、 停止()
如果结束
如果结束
下一个
端接头
专用子释放计时器()
'迭代我们的计时器列表。
对于每个c作为计时器中的计时器
'虽然可能,但此时您可以相当肯定计时器不是空的。
c、 开始()
下一个
端接头
正如您可能已经注意到的,我还使用了
而不是
。这是因为如果左侧(
c.Enabled
)的计算结果为True,则
仅计算右侧(
c.Interval 100
),而
将始终计算两侧


这就是所谓的短路,您可以在这里了解更多信息:

非常感谢,非常好的解释!
'Our list of timers.
Dim Timers As New List(Of Timer)

Private Sub blocktimers()
    'Clear the list before we start adding timers (just in case you decide to call this method multiple times).
    Timers.Clear()

    For Each c As Object In Me.components.Components
        If TypeOf c Is Timer Then
            If c.Enabled AndAlso c.Interval <> 100 Then
                'Cast 'c' to a Timer and add it to the list.
                Timers.Add(DirectCast(c, Timer))
                c.Stop()
            End If
        End If
    Next
End Sub

Private Sub releasetimers()
    'Iterate our Timers list.
    For Each c As Timer In Timers
        'While possible, you can be fairly certain at this point that a timer is not null.
        c.Start()
    Next
End Sub