MikeB
9/27/2002 4:42:00 PM
"Fred Truter" <fred@e-bytesafe.com> wrote in message
news:eT4YSQ$YCHA.4264@tkmsftngp08...
> The .NET documentation says you must declare a handler of the form
>
> public static void OnTimer(object source, ElapsedEventArgs e)
>
> However, it doesn't say what "source" is.
>
> 1) Is it the Timer object which fires the event?
>
> 2) Or can it be the object of which OnTimer is a member? This would be
very useful since static member functions have no reference to any
particular instance of the class to which they belong. OnTimer is of course
static.
>
> 3) Or is it some other useless System object?
>
> All the samples simply show the OnTimer handler writing something to the
console, which is fine if you get a kick out of writing Hello World
applications.
>
> I assumed that (2) above is the most probable, so I cast the source
argument to the class type and tried to access other members, but this
results in random crashes. If I never start the timer, then the program
never crashes. If I start the timer, but simply write to the console, its
also fine. But I want more...
>
> Please help.
You're right - the System.Timers.Timer doesn't seem to provide a way to pass
context to the event that gets called when a timer has fired. All you seem
to be able to get is the amount of time that has elapsed which caused the
timer to fire the event.
You should look at the System.Threading.Timer class, which does allow you to
pass in an arbitrary object that the timer callback can use to get context
information.
--
MikeB