how implement busy waiting in not total inefficient way? facing issue can load data of model in pull manner, means have invoke getxyz() methods in continuous way.
this has happen not fast enough user interaction, fast enought, when state in gui changed, model can noticed , new state received getxyz() methods.
my approach be:
while (c.haschanged()) { thread.sleep(500); } updatedata();
are there better mechanisms?
your problem seems solvable threading.
in wpf can do:
thread t = new thread((threadstart)delegate() { while (true) { thread.sleep(500); if (c.haschanged()) dispatcher.invoke((action)delegate() {updatedata();}); } }).start();
in winforms
thread t = new thread((threadstart)delegate() { while (true) { thread.sleep(500); // must derive control if (c.haschanged()) this.invoke((action)delegate() {updatedata();}); } }).start();
there may missing parameters invoke (which needed execute code on calling ui thread) i'm writing brain no intellisense @ disposal :d
in .net 4 can use taskfactory.startnew instead of spawning thread yourself. in .net <= 4, use treadpool thread. recall need run @ once because expect there checking possible , thread pool won't assure (it full, not likely:-). don't silly things spawning more of them in loop!
and inside thread should put check
while (!closing)
so thread can finish when need without having resort bad things t.abort();
when exiting put closing true , t.join()
close checker thread.
edit:
i forgot closing should bool property or volatile boolean, not simple boolean, because won't ensured thread ever finish (well in case closing application, practice make them finish will). volatile keyword intended prevent (pseudo)compiler applying optimizations on code assume values of variables cannot change
Comments
Post a Comment