c# - Parallel.Foreach and Task conflict -
i uploading images cloud in parallel execution :
// make taskfactory use ui thread's context var uifactory = new taskfactory(taskscheduler.fromcurrentsynchronizationcontext()); parallel.foreach(finalfilenames, new paralleloptions { maxdegreeofparallelism = 4 }, path => { count++; /* calculate percentage of upload done */ double ipercentdone = (int)(((float)count / itotalfiles) * 100); // send progress report ui thread. uifactory.startnew(() => uploadprogress.value = ipercentdone; lblprogress.content = count.tostring(cultureinfo.invariantculture) + " file(s) uploaded " + itotalfiles + " file(s)"; } });
the problem facing is, ui blocked when doing this. reason seems working in same thread..
if wrap parallel.foreach in "task.factory.new", making async calls, not requirement.
please let me know, how can fix ui block issue, not making calls async.
the parallel.foreach
when started ui thread block ui thread until finished, tasks started queue not run until ui thread returns message loop.
one way run parallel.foreach
in thread using task.run()
, better way use async/await in combination iprogress<t>
see
Comments
Post a Comment