I need to call a function on a set timer. Currently I'm using the system.timers.timer sample code from http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed(v=vs.110).aspx
iaTimer = New System.Timers.Timer(60000)
'Hook up the Elapsed event for the time
AddHandler aTimer.Elapsed, (AddressOf OnTimedEvent)
aTimer.Enabled = True
'The event handler for the Timer.Elapsed event.
Public Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs, ByVal companies() As companyAccount)
timermain(companies)
End Sub
I do not know how to pass parameters to the delegate. The array companies() is an array of companyAccount objects which are basically structs. I need these parameters to be fed to the timermain function. Elsewhere in the code I have a delegate that accepts parameters to update textboxes, So this should be possible.
I think this question is similar. How to pass the sender parameter to the system.timers.timer
You cannot change the Timer.Elapsed
event signature (or generally change the signature of events) to pass additional parameters. Instead, you should declare your companies
variable in a scope that will be accessible to the event handler.
You can do this by making it class-level, if all this code is in the same class.
Class SomeClass
Private companies() As CompanyAccount
Sub SetupTimer()
iaTimer = New System.Timers.Timer(60000)
' Hook up the Elapsed event for the time
AddHandler aTimer.Elapsed, (AddressOf OnTimedEvent)
aTimer.Enabled = True
End Sub
' The event handler for the Timer.Elapsed event.
Public Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
timermain(companies)
End Sub
End Class
You may also be able to use a lambda expression:
Dim companies() As CompanyAccount
companies = ...
iaTimer = New System.Timers.Timer(60000)
AddHandler aTimer.Elapsed, Sub(sender, e) timermain(companies)
aTimer.Enabled = True
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments