.NET System.Timers.Timer How to pass parameters to a delegate function?

Edeast

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

pmcoltrane

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No overload for matches delegate 'System.Timers.ElapsedEventHandler'

From Dev

No overload for matches delegate 'System.Timers.ElapsedEventHandler'

From Dev

How to pass function as parameters

From Dev

Pass diverse parameters to a delegate

From Java

System.Timers.Timer vs System.Threading.Timer

From Dev

System.Timers.Timer steadily increasing the interval

From Dev

system.timers.timer autoreset default value

From Dev

System.Timers.Timer strange behaviour

From Dev

System.Timers.Timer gradually increasing interval?

From Dev

A delegate for a function with variable parameters

From Dev

Replacing the System.Timers.Timer.Elapsed event when inheriting System.Timers.Timer

From Dev

Countdown timer in a thread using System.Timers.Timer

From Dev

How to prevent a C# System.Timers timer event from blocking subsequent events?

From Dev

System.Timers.Timer: How to raise elapsed event after start interval?

From Dev

How to pass positional parameters to a function

From Dev

php - How to pass parameters to a function

From Dev

Pass System.Threading.Timer object reference to its callback function

From Dev

How to pass a function with parameters with a function in python?

From Dev

How to pass parameters to Linux system call?

From Dev

Is it okay to attach async event handler to System.Timers.Timer?

From Dev

System.Timers.Timer elapsed event fires too early

From Dev

Simple example of the use of System. Timers. Timer in C#

From Dev

Does System.Timers.Timer terminates on aborting its working Thread?

From Dev

C# - System.Timers.Timer will not turn off

From Dev

How to pass a function func as argument to another and then assignate this argument to a delegate variable?

From Dev

Creating delegate function that takes parameters

From Dev

.NET C++: How to bind parameters to a delegate to create a new a zero-parameter delegate?

From Dev

How do Timers work in .net?

From Dev

Swift - How can I pass a function with parameters in parameters of a function?

Related Related

  1. 1

    No overload for matches delegate 'System.Timers.ElapsedEventHandler'

  2. 2

    No overload for matches delegate 'System.Timers.ElapsedEventHandler'

  3. 3

    How to pass function as parameters

  4. 4

    Pass diverse parameters to a delegate

  5. 5

    System.Timers.Timer vs System.Threading.Timer

  6. 6

    System.Timers.Timer steadily increasing the interval

  7. 7

    system.timers.timer autoreset default value

  8. 8

    System.Timers.Timer strange behaviour

  9. 9

    System.Timers.Timer gradually increasing interval?

  10. 10

    A delegate for a function with variable parameters

  11. 11

    Replacing the System.Timers.Timer.Elapsed event when inheriting System.Timers.Timer

  12. 12

    Countdown timer in a thread using System.Timers.Timer

  13. 13

    How to prevent a C# System.Timers timer event from blocking subsequent events?

  14. 14

    System.Timers.Timer: How to raise elapsed event after start interval?

  15. 15

    How to pass positional parameters to a function

  16. 16

    php - How to pass parameters to a function

  17. 17

    Pass System.Threading.Timer object reference to its callback function

  18. 18

    How to pass a function with parameters with a function in python?

  19. 19

    How to pass parameters to Linux system call?

  20. 20

    Is it okay to attach async event handler to System.Timers.Timer?

  21. 21

    System.Timers.Timer elapsed event fires too early

  22. 22

    Simple example of the use of System. Timers. Timer in C#

  23. 23

    Does System.Timers.Timer terminates on aborting its working Thread?

  24. 24

    C# - System.Timers.Timer will not turn off

  25. 25

    How to pass a function func as argument to another and then assignate this argument to a delegate variable?

  26. 26

    Creating delegate function that takes parameters

  27. 27

    .NET C++: How to bind parameters to a delegate to create a new a zero-parameter delegate?

  28. 28

    How do Timers work in .net?

  29. 29

    Swift - How can I pass a function with parameters in parameters of a function?

HotTag

Archive