Array and multilpe timers

ishadowme
ishadowme used Ask the Experts™
on
Hi,

I have a dynamic string array which contains difference duration in milliseconds:
Dim strDuration() As String
Dim i As Integer
Open "C:\DURATION.TXT" For Input As #1
Do While Not EOF(1)
    ReDim Preserve strDuration(i)
    Input #1, strDuration(i)
    i = i + 1
Loop
Close #1

And I have a timer class which was used API to create with. The main method is
Timer.SetTimer(millsec) ' the timer will go off in number of milliseconds

My first question is if it's possible having each independent timer for each array?
If it's possible then my second question is how can I set each timer in each duration array? And after each timer goes off it will reset and restart again.
Just like having multiple stop-watches for each individual athlete
I'm thinking of For Each statement but I'm not sure how to implement that.
Would any expert give me some examples, guild line, or suggestions?

Thanks in advance
Andrew
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
What API are you using to create the Timers?  This will have a huge impact on whether you can create seperate Timers.

We can't really answer the question without more info...

Author

Commented:
Hi Idle Mind,

The Timer include a class and a module. I did not make them, I got them from the net. Here they are:
Class
Option Explicit

Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Public Event Timer()

Private TimerID As Long

Public Property Get IsSet() As Boolean

   IsSet = TimerID <> 0

End Property

Public Function SetTimer(Wait As Long) As Boolean

   CancelTimer
   TimerID = SetTimer(0, 0, Wait, AddressOf TimerProc)
   SetTimer = TimerID <> 0

End Function

Public Function CancelTimer() As Boolean

   If TimerID <> 0 Then
      If KillTimer(0, TimerID) <> 0 Then
         TimerID = 0
         CancelTimer = True
      End If
   End If

End Function

Friend Sub RaiseTimer()

   CancelTimer
   RaiseEvent Timer

End Sub

Private Sub Class_Initialize()

   Set mTimer = Me

End Sub

Private Sub Class_Terminate()

   CancelTimer
   Set mTimer = Nothing

End Sub

Module
Option Explicit

Public mTimer As TimerClass

Public Function TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long) As Long

   mTimer.RaiseTimer

End Function
Commented:

You could create a new instance of the timer class, but in your code this would not work, since every class references the global object mTimer. This is not reentrant, because you would have only one timer in the whole application.
So, you can download this code

http://www.vbaccelerator.com/home/VB/Code/Libraries/Subclassing/SSubTimer/article.asp

that contains a timer class (similar to yours) and then you can do this

HTH
Marco
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
It is actually possible to have multiple timers using the SetTimer() API:
http://msdn2.microsoft.com/en-us/library/ms644906.aspx

    "If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer."

I would just get rid of the Class and track your Timer IDs in a Public Collection so you can kill them all when your app closes.

In your TimerProc() sub, use the "idEvent" parameter to figure out which timer fired.

Author

Commented:
thanks guys

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial