c# - Understanding the WPF Dispatcher.BeginInvoke -


i under impression dispatcher follow priority of operations queued , execute operations based on priority or order in operation added queue(if same priority) until told no case in case of wpf ui dispatcher.

i told if operation on ui thread takes longer duration database read ui dispatcher simple tries execute next set of operations in queue. not come terms decided write sample wpf application contains button , 3 rectangles, on click of button, rectangles filled different colors.

<stackpanel>     <button x:name="fillcolors" width="100" height="100"              content="fill colors" click="onfillcolorsclick"/>     <textblock width="100" text="{binding order}"/>     <rectangle x:name="rectangleone" margin="5" width="100" height="100" fill="{binding brushone}" />     <rectangle x:name="rectangletwo" margin="5" width="100" height="100" fill="{binding brushtwo}"/>     <rectangle x:name="rectanglethree" margin="5" width="100" height="100" fill="{binding brushthree}"/> </stackpanel> 

and in code-behind

private void onfillcolorsclick(object sender, routedeventargs e) {     var dispatcher = application.current.mainwindow.dispatcher;      dispatcher.begininvoke(new action(() =>     {         //dispatcher.begininvoke(new action(setbrushonecolor), (dispatcherpriority)4);         //dispatcher.begininvoke(new action(setbrushtwocolor), (dispatcherpriority)5);         //dispatcher.begininvoke(new action(setbrushthreecolor), (dispatcherpriority)6);          dispatcher.begininvoke(new action(setbrushonecolor));         dispatcher.begininvoke(new action(setbrushtwocolor));         dispatcher.begininvoke(new action(setbrushthreecolor));      }), (dispatcherpriority)10); }  private void setbrushonecolor() {     thread.sleep(10 * 1000);     order = "one";     //messagebox.show("one");     brushone = brushes.red; }  private void setbrushtwocolor() {     thread.sleep(12 * 1000);     order = "two";     //messagebox.show("two");     brushtwo = brushes.green; }  private void setbrushthreecolor() {     thread.sleep(15 * 1000);     order = "three";     //messagebox.show("three");     brushthree = brushes.blue; }  public string order {     { return _order; }     set     {         _order += string.format("{0}, ", value);         raisepropertychanged("order");     } } 

the commented code works expected methods invoked based on dispatcherpriority , see screen refresh after each operation has been completed. order one, two, three. colors drawn 1 after another.

now working code dispatcherpriority not mentioned ( assume default normal) order still one, two, three if show messagebox inside methods, the
thrid popup show first two one when debug see methods are
invoked in expected order (intellitrace shows message box shown don't see on screen @ time , see after last operation finished.) messageboxes shown in reverse order.

is because messagebox.show blocking call , operation cleared after message has been closed.
order of messagebox should one, two andthree` ?

before coming down code behavior it's prerequisite understand priorities of dispatcher. dispatcherpriority divided ranges shown in below image.

dispatcherpriority

if queue 4 actions 4 above ranges on dispatcher. foreground queue executed first, background , in last idle queue. priority 0 not executed.

now code:

three task queued 1st in background, 2nd in background , 3rd in foreground queue. 3rd executed first. 2nd task cause has higher priority 1st task. hope clears it.

although more observation understand better like, if have set priorities 7,8 , 9. foreground queue, 7 executed first 7 , 8. 1 one , exclusively in order , while 7 getting executed, 8 , 9 wait, meaning foreground queue executed synchronously each another.

but background , idle queue not behave in way execution asynchronous other tasks , tasks follow priority. , first background , idle queue.

hope explanation clarifies extent.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -