469,942 Members | 2,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,942 developers. It's quick & easy.

passing argument to timer event

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??
Nov 17 '05 #1
2 9148
Thorsten moeller wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #2
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

"Oliver Sturm" wrote:
Thorsten moeller wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
reply views Thread by Jeffrey B. Holtz | last post: by
1 post views Thread by Kerry Jenkins | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.