c# - Thread.Sleep not working! -
consider following code executed in every instantiation of class:
private void startupdatethread() { runupdatethread = true; thread thread = new thread(new threadstart(updateusages)); thread.priority = threadpriority.belownormal; thread.start(); } public void updateusages() { datetime updatedatetime = datetime.now; while (runupdatethread) { if (updatedatetime <= datetime.now) { _cpuusage.add(datetime.now, getcpuusage()); if (_cpuusage.count == 61) _cpuusage.remove(_cpuusage.elementat(0).key); _ramusage.add(datetime.now, getramusage()); if (_ramusage.count == 61) _ramusage.remove(_ramusage.elementat(0).key); updatedatetime = datetime.now.addseconds(15); } thread.sleep(15000); } }
after adding 2 or 3 values each dictionary throws "an element same key exists in dictionary". should impossible since i'm doing sleep after each loop. i've tried, unsuccessfully, prevent problem adding updatedatetime variable.
i'm running out of ideas. can me or explain me how can happen?
thanks
are either _cpuusage
or _ramusage
static chance? or perhaps you've assigned same value both of them? if give short complete program demonstrates problem, make things lot clearer.
on side note, seem hoping usage of elementat(0)
remove earliest entry dictionary, there's no guarantee of that.
from you're doing, looks you'd better off linkedlist<tuple<datetime, long>>
or similar.
Comments
Post a Comment