Using System.Timers.ElapsedEventHandler to specify a method and and ElapsedEventArgs object

  • Thread starter Thread starter Max Adams
  • Start date Start date
M

Max Adams

Using System.Timers.ElapsedEventHandler to specify a method and and
ElapsedEventArgs object

I've trawled the internet looking for some help on this topic. What I want
to do is, every x seconds call function y with some parameters. Simple.
The ElapsedEventArgs paramerter I want to pass in is an object "c" of a
custom type.
The following does not work:

Clock.Elapsed += new System.Timers.ElapsedEventHandler( OnTimer, new
ElapsedEventArgs( c ) );

"Method 'frmMain.OnTimer(object, System.Timers.ElapsedEventArgs)' referenced
without parentheses

I have put () after OnTimer but to no avail, can anyone point me to a
reliable sample which passes this second parameter in.

PT
 
Max,

When you set the event handler to your code, you are setting the method
that will be called. You can't set the parameters that are passed to it.
That is reserved for the object firing the event. In this case. What you
want to do is make sure that your OnTimer method has access to the variable
"c" (store it on the class level, create a new instance, etc, etc). Then,
when your method is called, you pass "c" to whatever method you are going to
call in the event handler.

Hope this helps.
 
Back
Top