0

I'm trying to loop all timers and stop all of them but the one I'm running on.

Code

'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

blocktimers() is looping around all timers but releasetimers() only loop around 2 timers and the 2nd timer value is: Nothing

If blocktimers() loops for example 10 timers, releasetimers() is only looping 1.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78

1 Answers1

1

You forgot to increment i after adding a timer to the array:

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

i += 1 '<-- You forgot this.

Also this line needs to be changed:

For Each c As Timer In Me.components.Components

This assumes that every component is a timer, meaning your code will break if you any component that isn't that.

Change it to this instead:

For Each c As Object In Me.components.Components

Suggestion:

Since you are using a somewhat dynamic sized array anyway I suggest you switch to a List(Of T) to save yourself some headache:

'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

As you may have noticed I also used AndAlso instead of And. This is because AndAlso only evaluates the right side (c.Interval <> 100) if the left side (c.Enabled) evaluates to True, whereas And will always evaluate both sides.

This is known as short-circuiting, which you can read more about here: (OrElse and Or) and (AndAlso and And) - When to use?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75