c# - Task.Parallel updating UI. Why So? -


i able update ui code 1 not 2.

code 1

   parallel.foreach(names, name =>    {      lbltext.text += "\n" + name + " thread " + thread.currentthread.managedthreadid;    }); 

code 2

 task.factory.startnew(() =>             {                 parallel.foreach(names, name =>                 {                     lbltext.text += "\n" + name + " thread " + thread.currentthread.managedthreadid;                 });             }); 

i know code 2 won't update ui because secondary thread. why code 1 updating ui? don't parallel foreach run different threads? if yes why updating ui?

output of code 1

enter image description here

both code segments work... really.

the problem first code block runs while blocks ui thread. second code block starts task , continues.

the problem isn't in use of multiple threads, since both examples use multiple threads change value of label. problem in state of form.

i assume run code in form constructor. in first case, there no handle created, the operation doesn't need ui thread. updates backing value. in second case, in split millisecond needs create task, creates handle form. when needs update label the operation needs ui thread.

if put in wait on task see work. if move code onhandlecreated, both statements fail.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -