c# - Why would a long running Task still block the UI? -


i'm trying resolve problem ui being blocked , don't understand why.

public task addstuff(string myid, list<string> otherids) {     action doit = () =>     {         this.theservice.addstuff(myid, otherids);     };      return task.factory.startnew(doit, taskcreationoptions.longrunning); } 

if list long call can take 30 seconds , entire application becomes unresponsive (goes washed out white in windows 7).

is there different way doesn't block ui?


edit

ok, there's lot of code around i'm going try keep pertinent. did realize going original code, had removed may have been important. should maybe use different taskscheduler taskscheduler.current?

also there no wait statements impeding of code, , service doesn't interact ui.

task.factory.startnew(objstate =>     {         loadassets(objstate);     }, state, this.cancellationtoken, taskcreationoptions.longrunning, taskscheduler.current); 

private void loadassets(object objstate) {     loadassetsstate lastate = (loadassetsstate)objstate;      list<string> assetids = new list<string>();      (int = 0; < lastate.addedmediaitems.count; i++)     {         if (lastate.cancellationtoken.iscancellationrequested)             return;          string assetid = this.selectfilesstep.assetservice.getassetid(lastate.addedmediaitems[i], lastate.activeorder.orderid);          assetids.add(assetid);      }      if (lastate.cancellationtoken.iscancellationrequested)         return;      this.apicontext.addassettoproduct(lastate.activeorder.orderid, lastate.activeproduct.lineid, assetids, lastate.quantity, lastate.cancellationtoken).continuewith(task =>     {         if (lastate.cancellationtoken.iscancellationrequested)             return;           app.apicontext.getorderdetails(lastate.activeorder.orderid, false, lastate.cancellationtoken).continuewith(orderdetailstask =>         {             if (lastate.cancellationtoken.iscancellationrequested)                 return;              this.activeorder = orderdetailstask.result;              this.standardprintproductsstep.synchronize(this.activeorder);          });     }); } 

public task addassettoproduct(string orderid, string lineid, list<string> assetids, int quantity, cancellationtoken? cancellationtoken = null) {     action doit = () =>     {         if (cancellationtoken.iscancellationrequested())             return;          this.ordersservice.addassettoproduct(orderid, lineid, assetids, quantity);     };      if (cancellationtoken != null)         return task.factory.startnew(doit, cancellationtoken.value, taskcreationoptions.longrunning, taskscheduler.current);     else         return task.factory.startnew(doit, taskcreationoptions.longrunning); } 

edit

i have placed break points before , after service call , service call blocking ui, opposed other line.

it sounds there no reason should blocking, think i'm going break list down if it's long , make multiple calls. wanted make sure wasn't missing here task logic.

is there different way doesn't block ui?

this call, in , of itself, should not block ui. if, however, theservice.addstuff synchronization ui's synchronizationcontext, cause ui blocked call.

otherwise, problem happening outside of function. example, if call wait() on task returned method, in ui thread, ui thread blocked until completes.


you want use taskscheduler.default, not taskscheduler.current. if being called within task that's scheduled on taskscheduler based on ui thread, schedule on ui thread.


Comments