arrays - Remove a Timer from List<Timer> in runtime C# -
i'm trying remove timer component list -
foreach (timer timer in timers) { if (timer.tag.tostring()=="mytag") { timer.enabled = false; timer.tick -= ontimertick; timers.remove(timer); } }
but gives error-
collection modified; enumeration operation may not execute.
any help?
for unknown size should use list instead of array.
try list<timer> timers = new list<timer>();
[edit] changed whole question new 1 after answer. can't remove item inside foreach loop. use duplicate list or iterate backwards:
for (int = timers.count - 1; >= 0; i--) { if (timers[i].tag.tostring()=="mytag") { timers[i].enabled = false; timers[i].tick -= ontimertick; timers.removeat(i); } }
Comments
Post a Comment