OK, I asked this question a couple of weeks ago, and I was given an answer that suited me at the time, but my requirements have changed a bit. I would like to know when a time.elapsed fires, which timer it was that fired. Previously it was good enough to just look through my timer dictionary for a timer with the same parameters as the one sent along with the event. But I have seen that I may have multiple timers with the same parameters. So I decided to roll my own timer class which inherits from System.Timers.Timer, and adds a couple of items. (In case you are wondering, I am writing a program that calculates ISS flyovers and generates notifications for the flyovers)
Here's how I implement itCode:class FlyoverNotification : System.Timers.Timer
{
public System.DateTime targetNotificationTime = new DateTime();
public string key = "";
}
And here's the event handler, which doesn't workCode:if (!_flyoverNotificationList.ContainsKey(k))
{
FlyoverNotification n = new FlyoverNotification();
n.Elapsed += new System.Timers.ElapsedEventHandler(UpcomingFlyover);
n.Interval = interval;
n.Enabled = true;
n.AutoReset = false;
n.targetNotificationTime = isc.flyoverStart - ts;
n.key = k;
_flyoverNotificationList.Add(k, n);
}
The problem is, when the event handler is fired, if I put a debugging breakpoint on the var firedtimer = line, I do see that Object sender does contain the correct FlyoverNotification parameters, such as sender.key. However, I cannot put sender.key in the LINQ where clause. Do I need to somehow cast the sender as a FlyoverNotification object? Or is my approach all wrong?Code:public void UpcomingFlyover(Object sender, System.Timers.ElapsedEventArgs e)
{
var firedtimer = (from tmpflyover in _flyoverNotificationList
where tmpflyover.Value.notificationTimer.Equals(sender)
select tmpflyover).FirstOrDefault();
// do other stuff
}

