Array of System.Timers.timer - Assigning an object to timer

G

Gina_Marano

Good day,

I read 1-n records from a database, For each record in the database I
create a system timer. (then add the timer to a list of timers).

I create an object to store the data for each record in:

public class CustAckAttribs
{
public int CustomerID;
...
}

When the timer elapses how do I know what data to use? How do I assign
the specific object to the timer created for it?

thanks

~Gina_M~
 
M

Marc Gravell

Well, you could hook the timer's event to a method on the class in
question (or a proxy) - but I must say: that sounds like a lot of
timers... something akin to below. Note also that "as is" this will
prevent custAck from being garbage collected until it has fired... you
might want a WeakReference in there somewhere...

CustAckAttribs custAck = new CustAckAttribs();
Timer timer = new Timer(TIMEOUT);
globalTimers.Add(Timer);
// using a proxy (inline delegate will serve quite nicely)
timer.Elapsed += delegate {
custAck.TimerElapsed1();
};
// using direct method
timer.Elapsed += custAck.TimerElapsed2;
 
G

Gina_Marano

Thanks Marc,

This and a previous post that was answered got me there. If I can
cleanup (and take out anything priorietary) I will post the solution
here.

~Gina_M~
 

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

Top