passing argument to timer event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i am am fairly new to c# and trying to write a service to check some thing
periodically. i want to use a windows service. The onStart method is starting
a timer. The timer.elapsed event creates a new Handler, which is calling
another method. Is it possible to give this method an argument?

Code now:
timer.Elapsed += new ElapsedEventHandler(requestStat);

Code desired:
timer.Elapsed += new ElapsedEventHandler(requestStat(conn));

How can this be made??
 
Thorsten said:
i am am fairly new to c# and trying to write a service to check some thing
periodically. i want to use a windows service. The onStart method is
starting
a timer. The timer.elapsed event creates a new Handler, which is calling
another method. Is it possible to give this method an argument?

Code now:
timer.Elapsed += new ElapsedEventHandler(requestStat);

Code desired:
timer.Elapsed += new ElapsedEventHandler(requestStat(conn));

How can this be made??

That's not possible, you have to use the event handler as it is. You'll
need to detect your state either from the arguments that the event handler
gets passed in by default or from other information available to the
object at that moment.

If you're actually willing to fix the parameter to pass to the event
handler at the point where you hook it up (as your sample suggests), this
shouldn't really be a problem, is it?


Oliver Sturm
 
Thanks Oliver!

What came to my mind just when i read your mail wa,s that i have to make a
declaration not in the OnStart method of the service, but in class definition
part.
Now it compiles ok.

Thanks

thorsten
 
Back
Top