与超时列表/到期的能力 [英] List with timeout/expire ability
本文介绍了与超时列表/到期的能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这样的功能:
AddToList(txtName, timeExpire);
它看起来像自我描述,该项目将自动过期,并从列表中删除。
It looks like self-descriptive, the item will automatically expire and removed from the list.
我无法想象如何做到这一点。谁能给我一个线索?
I couldn't imagine how to do that. Can anyone give me a clue?
推荐答案
您也可以尝试这样的事情。
you can also try something like this.
创建一个自定义类
public class Custom
{
string item; //will hold the item
Timer timer; //will hanlde the expiry
List<Custom> refofMainList; //will be used to remove the item once it is expired
public Custom(string yourItem, int milisec, List<Custom> refOfList)
{
refofMainList = refOfList;
item = yourItem;
timer = new Timer (milisec);
timer.Elapsed += new ElapsedEventHandler(Elapsed_Event);
timer.Start();
}
private void Elapsed_Event(object sender, ElapsedEventArgs e)
{
timer.Elapsed -= new ElapsedEventHandler(Elapsed_Event);
refofMainList.Remove(this);
}
}
现在,您可以创建列表<定制>
并使用它。它的项目将被删除,一旦指定的时间已经结束了。
Now you can create List<Custom>
and use it. It's items will be deleted once specified time is over.
这篇关于与超时列表/到期的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文