C# 如何检测此场景中的项目是否已删除
我有一个场景,在一个连续运行的线程中,“timer”调用我的函数“custommethod”。我无法控制系统代码这只是为了演示系统是如何工作的,我甚至不知道在系统代码中编写了哪些代码 系统代码C# 如何检测此场景中的项目是否已删除,c#,asp.net,vb.net,winforms,c#-4.0,C#,Asp.net,Vb.net,Winforms,C# 4.0,我有一个场景,在一个连续运行的线程中,“timer”调用我的函数“custommethod”。我无法控制系统代码这只是为了演示系统是如何工作的,我甚至不知道在系统代码中编写了哪些代码 系统代码 timer duration = 1000 void timer_elapsed() { foreach(string symbol in symbols) //symbol list may vary { cus
timer duration = 1000
void timer_elapsed()
{
foreach(string symbol in symbols) //symbol list may vary
{
custommethod(symbol);
}
}
终端系统代码
所以,我的方法“custommethod”只是周期性地从系统代码中获取一个参数。当用户添加或删除由系统代码处理的符号时,符号列表可能会有所不同。我得到的只是周期性的活动符号。所以,我关心的是保留一个活动符号列表
我的代码
结束我的代码
这就是我迄今为止所尝试的
HashTable ht=new HashTable();
function custommethod(string symbol)
{
//Check whether new symbol is added or a symbol is deleted
if(!ht.ContainsKey(symbol))
{
ht.Add(symbol,0); //New symbol added
}
//How will i come to know whether a symbol is deleted or not
}
如果
ht
是相同的symbols
,那么就不好了。决不能对foreach
循环的源进行变异
如果是这种情况,您应该生成一个新集合,然后用旧集合替换它。我不会使用“for each”方法,而是使用HashSet在新集合和旧集合之间进行集合比较(虽然这大部分是Linq,所以HashTable可以很好,IDK)
正如我告诉你的,我对系统代码一无所知。这只是为了简化事情。我认为这里的问题是foreach是固定的。他没有办法修改那个部分。@ChrisO那么他就是SOL,因为如果他永远不知道集合的结束(界限),他就无法确定集合的差异。有没有办法让你知道计时器是否已经过了?计时器是否总是1秒?如果是这样,您可以存储日期时间。现在您将符号输入哈希表,然后在每次获得新符号时检查它是否过期。不,实际上,我使用windows消息传递向系统代码发送消息,该消息反过来使用活动符号调用custommethod函数,以便您知道/控制进程何时启动?如果是这样,在发送消息之前,可以清除哈希表。这样您就知道您拥有最新的符号列表。还是我遗漏了什么?好的。那么,当您启动进程并填充它时,您能创建一个新的临时哈希表吗?那么你可以做一个比较?
HashTable ht=new HashTable();
function custommethod(string symbol)
{
//Check whether new symbol is added or a symbol is deleted
if(!ht.ContainsKey(symbol))
{
ht.Add(symbol,0); //New symbol added
}
//How will i come to know whether a symbol is deleted or not
}
HashSet oldSet;//construct that correctly
HashSet newSet;//construct that correctly also
HashSet deletedSet = oldSet.Except(newSet);
HashSet addedSet = newSet.Except(oldSet);