c# - Efficiency of DispatcherTimer versus Thread.Sleep -
what more efficient use, dispatchertimer
or thread.sleep
in c#?
i use dispatchertimer
:
dispatchertimer timer = new dispatchertimer(); timer.interval = timespan.fromseconds(20); timer.tick += timer_tick; timer.start(); // every 20 seconds
or, use thread.sleep
:
while(true) { thread.sleep(20000); // every 20 seconds }
in cases should use them? there best practices?
edit: based on first answers, need add application developing has no ui (so blocking thread no issue), background tracker, writes data logfile.
the dispatchertimer
invoke method on ui thread every n [unit of time]. thread.sleep
sleep thread completely. difference former, thread continues, invokes tick
event , latter stops thread doing anything.
it's important realise ui thread busy rendering ui , running code; hence, sleep in ui thread perceived application hang.
Comments
Post a Comment