Timer Array

  • Thread starter Thread starter Scott McNair
  • Start date Start date
S

Scott McNair

Hi,

I'm trying to convert a VB ocx into a .NET dll. This dll ties into several
automated doors at our facility, and each door has a timer event attached
to it. When the user swipes his access card, he has a predetermined amount
of time (5 seconds) to open the door before the strike is re-enabled and
the door re-locks.

My problem is that I cannot declare an array of timers in .NET; I get the
error "'WithEvents' variables cannot be typed as arrays."

How would I work around this issue?

Regards,
Scott
 
My problem is that I cannot declare an array of timers in .NET; I get
the error "'WithEvents' variables cannot be typed as arrays."

How would I work around this issue?

Declare an array of timers, and then use AddHandler/RemoveHandler to
add/remove event registrations manually.
 
Declare an array of timers, and then use AddHandler/RemoveHandler to
add/remove event registrations manually.

I did that... I passed the following lines of code:

Dim X As Integer = 1
For Each T As Timer In tmrStrike
AddHandler T, tmrStrike_Timer(X)
X += 1
Next

tmrStrike_Timer (as was presented in the original code that I'm converting)
looks like this:

Private Sub tmrStrike_Timer(ByVal Index As Integer)
'Disable timer
tmrStrike(Index).Enabled = False

'Set output low
OUTPUT_STRIKE(Index, False)
End Sub

However I get an error on the AddHandler line, to the effect: "Expression
does not produce a value" which I'm currently researching.

Unfortunately, there are two arrays... one for each timer, and one for each
door; these are married to each other, so that timer(1) controls door(1),
etc. So I have to pass the index into the calling sub, unless I totally
want to retool the code (which I'm actively trying to avoid).
 
I did that... I passed the following lines of code:

Dim X As Integer = 1
For Each T As Timer In tmrStrike
AddHandler T, tmrStrike_Timer(X)
X += 1
Next

The reason it's not working is because your event registration signature
is wrong.

What kind of timer are you using?

System.Timer?
System.Threading.Timer?
System.Window.Forms.Timer?

Unfortunately, there are two arrays... one for each timer, and one for
each door; these are married to each other, so that timer(1) controls
door(1), etc. So I have to pass the index into the calling sub,
unless I totally want to retool the code (which I'm actively trying to
avoid).

Perhaps you should create a "Door" object and each door has it own
timer.

That way you don't need 2 arrays to handle what you want to do.

VB.NET is OO, so time to think in OO terms :-)
 
What kind of timer are you using?

System.Timer?
System.Threading.Timer?
System.Window.Forms.Timer?

Just a generic System.Window.Forms.Timer for this instance.
 
I did that... I passed the following lines of code:

Dim X As Integer = 1
For Each T As Timer In tmrStrike
AddHandler T, tmrStrike_Timer(X)
X += 1
Next

tmrStrike_Timer (as was presented in the original code that I'm converting)
looks like this:

Private Sub tmrStrike_Timer(ByVal Index As Integer)
'Disable timer
tmrStrike(Index).Enabled = False

'Set output low
OUTPUT_STRIKE(Index, False)
End Sub

You can not pass an arbitrary argument to the timer event. The timer
event routine is passed a reference to the timer as the 'sender'
argument, so one way to do this is to set a property of the timer to
your index value.


Dim X As Integer = 1
For Each T As Timer In tmrStrike
T.Tag = X
AddHandler T.Tick, AddressOf tmrStrike_Timer
X += 1
Next

Private Sub tmrStrike_Timer(sender as Object, _
e as System.Eventargs)
Dim timr as Timer = DirectCast(sender, Timer)
Dim Index As Integer = DirectCast(timr.Tag)

'Disable timer
timr.Enabled = False

'Set output low
OUTPUT_STRIKE(Index, False)
End Sub
 
Back
Top