获取具有计时器成员的 Class 项目列表中项目的索引
Get Index of an Item in a List of Class Items with Timer Member
我正在尝试查找触发计时器的索引。
我在 Program.cs
中创建了一个 Class 条目列表
static public List<Entry> Table = new List<Entry>();
这是名为 "Entry" 的 class,它的构造函数在 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());
列表将如下所示
列表索引 |参赛作品
0 |包| pktTimer
1 |包| pktTimer
2 |包| pktTimer
3 |包| pktTimer
4 |包| pktTimer
您非常接近的 IndexOf 需要您尝试获取其索引的项目。在这种情况下,您要获取索引的条目 class。我相信在你的情况下这将是关键字 this,所以 IndexOf(this).
https://msdn.microsoft.com/en-us/library/8bd0tetb(v=vs.110).aspx
@Jason Mastnick
导致我在给您的评论中提到的上述错误的原因是
Program.Table.RemoveAt(pktIndex); //Removes Entry at this Index
Program.Table[pktIndex].idleTimeOutTimer.Stop(); //Stops idleTimeOutTimer of This Entry
我应该先停止定时器,然后删除数据包
Program.Table[pktIndex].idleTimeOutTimer.Stop(); //Stops idleTimeOutTimer of This Entry
Program.Table.RemoveAt(pktIndex); //Removes Entry at this Index
顺便说一句,您的解决方案有效。我应该这样写。
pktTimer.Elapsed += (sender, e) => CallDeleteEntry(sender, e, Program.Table.IndexOf(this));
但是,出现了问题。我尝试按顺序在列表中添加条目。传递的第一个 pktIndex 是“1”而不是“0”。由于第一个项目是在索引 0 处添加的。在这个顺序场景中,它应该是第一个被删除的项目。一切正常,希望索引 0 处的第一项不会被删除。有什么想法吗?
我正在尝试查找触发计时器的索引。
我在 Program.cs
中创建了一个 Class 条目列表static public List<Entry> Table = new List<Entry>();
这是名为 "Entry" 的 class,它的构造函数在 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());
列表将如下所示
列表索引 |参赛作品
0 |包| pktTimer
1 |包| pktTimer
2 |包| pktTimer
3 |包| pktTimer
4 |包| pktTimer
您非常接近的 IndexOf 需要您尝试获取其索引的项目。在这种情况下,您要获取索引的条目 class。我相信在你的情况下这将是关键字 this,所以 IndexOf(this).
https://msdn.microsoft.com/en-us/library/8bd0tetb(v=vs.110).aspx
@Jason Mastnick
导致我在给您的评论中提到的上述错误的原因是
Program.Table.RemoveAt(pktIndex); //Removes Entry at this Index
Program.Table[pktIndex].idleTimeOutTimer.Stop(); //Stops idleTimeOutTimer of This Entry
我应该先停止定时器,然后删除数据包
Program.Table[pktIndex].idleTimeOutTimer.Stop(); //Stops idleTimeOutTimer of This Entry
Program.Table.RemoveAt(pktIndex); //Removes Entry at this Index
顺便说一句,您的解决方案有效。我应该这样写。
pktTimer.Elapsed += (sender, e) => CallDeleteEntry(sender, e, Program.Table.IndexOf(this));
但是,出现了问题。我尝试按顺序在列表中添加条目。传递的第一个 pktIndex 是“1”而不是“0”。由于第一个项目是在索引 0 处添加的。在这个顺序场景中,它应该是第一个被删除的项目。一切正常,希望索引 0 处的第一项不会被删除。有什么想法吗?