Avatar of 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,
Timer properties
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

.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon

>>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....
Jacques Bourgeois (James Burger)

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.

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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
Jacques Bourgeois (James Burger)

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Thank you, perfect! Did not know this was possible.