passing argument to timer event

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??
 
O

Oliver Sturm

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top