我正在尝试查找触发计时器的索引。
我在 Program.cs 中创建了一个类条目列表
static public List<Entry> Table = new List<Entry>();
这是名为“Entry”的类,其构造函数位于 Entry.cs
public class Entry
{
public int pktID;
public Timer pktTimer= new Timer();
}
public Entry()
{
}
public Entry(int _pktID, Boolean idleTimeOutStart)
{
this.pktID = _pktID;
if (idleTimeOutStart == true)
{
pktTimer.Elapsed += (sender, e) => CallDeleteEntry(sender, e, Program.Table.IndexOf());
pktTimer.Interval = 10000; // 10000 ms is 10 seconds
pktTimer.Start();
}
}
static void CallDeleteEntry(object sender, System.Timers.ElapsedEventArgs e, int pktIndex)
{
Program.Table.RemoveAt(pktIndex); //Removes Entry at this Index
Program.Table[pktIndex].idleTimeOutTimer.Stop(); //Stops idleTimeOutTimer of This Entry
}
列表中的项目是随机创建的。现在列表(列表索引)中的每个计时器都将启动,然后在 10000 毫秒后,将调用 CallDeleteEntry。
我需要做的是在计时器经过 10000 毫秒时将其索引传递给 CallDeleteEntry,这样它就可以删除列表的该项目行。
我认为必须在此处修改某些内容才能使其正常工作。
idleTimeOutTimer.Elapsed += (sender, e) => CallDeleteEntry(sender, e, Program.Table.IndexOf());
列表将如下所示