Link to home
Start Free TrialLog in
Avatar of jxharding
jxharding

asked on

Create Custom Windows Service Timer : Basically Add Name to Timer

I have a function where I pass a Timer, e.g.

Private Sub Process(myTimer as timer)
  Select Case myTimer.ToString()
        Case "Timer1"
        Case "Timer2"
        Case "Timer3"
  End Select
End Sub

Open in new window


However, when I pass a specific Timer- I cannot figure out inside Process() which Timer is currently processing.

Timer has the properties,
User generated image
How can I get the name of the Timer?

How about a Custom Timer? - how do i set the Timer to MyTimer when creating a CustomTimer?
Public Class CustomTimer
  Inherits Timer

  Public Property TimerName As String
  Public Sub New(t As Timer)

  End Sub
End Class

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>I cannot figure out inside Process() which Timer is currently processing.

erm, isn't Timer1 being processed when the case for Timer1 runs, similarly for Timer2....
It looks like you are using a System.Windows.Forms.Timer and not a System.Timer.

In such a case, try using the Name property of the Timer instead of calling ToString.

The Name property is automatically set to the name that you give to the timer when you add it to a Windows Form in the Form Designer. But if you create the timers dynamically, it is left empty. If this is the case, then you need to assign yourself a value to the Name property after creating the timer.
Avatar of jxharding
jxharding

ASKER

Thank you for the replies.I checked now - it is a System.Timers.Timer (i def could have overlooked this so thanks for the advice Jacques). It does seem that other than the declaration of a timer - there is no name parameter that gets passed.

I made a custom Timer, that inherits from Timer - and then loops through all the properties and sets the custom timer properties = properties of the timer that I pass to it , + add TimerName property.

It does work, but I wonder if I'm doing it properly?
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, perfect! Did not know this was possible.