resourcesys
asked on
Disable and re-enable a dynamic System.Timers.Timer
Hello,
We have a windows service that creates several timers dynamically.
They execute every 20 seconds. sometimes this is enough time to do the processing, sometimes not.
So, we would like to be able to disable the timer, then re-enable it after processing has finished.
I tried creating a global list of timers and adding each timer to the list, then setting the properties, but this did not work.
All input is greatly appreciated.
resourcesys.
We have a windows service that creates several timers dynamically.
They execute every 20 seconds. sometimes this is enough time to do the processing, sometimes not.
So, we would like to be able to disable the timer, then re-enable it after processing has finished.
I tried creating a global list of timers and adding each timer to the list, then setting the properties, but this did not work.
All input is greatly appreciated.
resourcesys.
Please post your code so we can see what you are working with so we can suggest some solutions.
ASKER
Hello Fernando,
Firstly, using a repository object we get a list of classes (machine)
Then we have the method that carries out the processing:
It's as simple as that.
I have tried disabling objTimer but that doesn't work it seems to disable all timers?
Thanks.
resourcesys
Firstly, using a repository object we get a list of classes (machine)
Dim lstMachines As List(Of Machine) = objMachiness.GetMachines()
For i As Integer = 0 To lstMachines.Count - 1
Dim objTimer As New CustomTimer
objTimer.MachineObject = lstMachines.Item(i)
objTimer.Interval = 20000
AddHandler objTimer.Elapsed, AddressOf Timer_Elapsed
objTimer.Enabled = True
Next
Then we have the method that carries out the processing:
Private Sub Timer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
Dim objTimer As CustomerTimer = DirectCast(sender, CustomTimer)
'Processing here
End Sub
It's as simple as that.
I have tried disabling objTimer but that doesn't work it seems to disable all timers?
Thanks.
resourcesys
You probably want to implement a timer on each Machine object instead; e.g. -
-saige-
Imports System.Timers
Module Module1
Private machines As List(Of Machine)
Sub Main()
machines = (From i In Enumerable.Range(1, 20) Select New Machine() With {.ID = i, .Name = String.Format("Machine{0}", i), .CustomTimer = New Timer(i * 200)}).ToList()
For Each [machine] As Machine In machines
AddHandler [machine].TimerElapsed, AddressOf OnTimerElapsed
Console.WriteLine("Starting the timer for {0}", [machine].Name)
[machine].CustomTimer.Start()
Next
Console.ReadLine()
End Sub
Private Sub OnTimerElapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
If TypeOf sender Is Machine Then
Dim m = DirectCast(sender, Machine)
m.CustomTimer.Enabled = False
Console.WriteLine("Stopping the timer for {0}", m.Name)
End If
End Sub
End Module
Class Machine
Private ReadOnly fTimerElapsed As New List(Of ElapsedEventHandler)
Public Custom Event TimerElapsed As ElapsedEventHandler
AddHandler(value As ElapsedEventHandler)
fTimerElapsed.Add(value)
End AddHandler
RemoveHandler(value As ElapsedEventHandler)
fTimerElapsed.Remove(value)
End RemoveHandler
RaiseEvent(sender As Object, e As ElapsedEventArgs)
For Each handler In fTimerElapsed
Try
handler.Invoke(sender, e)
Catch ex As Exception
Debug.WriteLine(String.Format("Exception while invoking event handler: {0}", ex))
End Try
Next
End RaiseEvent
End Event
Private _customTimer As Timer
Public Property ID() As Integer
Public Property Name() As String
Public Property CustomTimer() As Timer
Get
Return _customTimer
End Get
Set(ByVal value As Timer)
_customTimer = value
If _customTimer IsNot Nothing Then
AddHandler _customTimer.Elapsed, AddressOf OnTimerElapsed
End If
End Set
End Property
Private Sub OnTimerElapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
RaiseEvent TimerElapsed(Me, e)
End Sub
End Class
Which produces the following output --saige-
ASKER
Hello it_saige,
Thank you for your response.
This looks promising, so I will do some testing of my own and give you feedback on how it goes.
Thanks.
resourcesys
Thank you for your response.
This looks promising, so I will do some testing of my own and give you feedback on how it goes.
Thanks.
resourcesys
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks it_saige, it worked great.
resourcesys.
resourcesys.