c# - Async operations and UI update -
in wpf application made of caliburn micro , telerik controls i've different screens load data remote service show data in gridview /fills comboboxes. i'm using sync/await operators make load retrieval operations i'm has bottleneck. doing await have main ui thread wait syncronization worker thread... consider sample
public class myviewmodel:screen { [omiss] public bool isbusy {get;set;} public list<foo> dropdownitems1 {get;set;} public list<foo2> dropdownitems2 {get;set;} public async void load() { isbusy =true; dropdownitems1 = await repository.loadstates(); dropdownitems2 = await repository.loadinstitutes(); isbusy = false; } }
in case i've first task loaded second no parallelism...how can optimize this? isbusy property that's bound via convention busy indicator how can set ? thanks
update #1:
i'n real code
public async task initcachedatatables() { var taskportolio = getportfolio(); var taskinstitutes = getinstitutes(); var taskstatus = getstatus(); var taskcounterparts = getcounterparts(); var taskcrosses = getcrosses(); var taskcurrencies = getcurrencies(); var tasksigns = getsigns(); await taskex.whenall(new[] { taskportolio, taskinstitutes, taskstatus, taskcounterparts, taskcrosses, taskcurrencies, tasksigns }); }
where tasks like
private async task getportfolio() { try { dynamiccontainer.setvalue(usercontainerkeyhelper.portfolios, await commonrepository.getportfoliosasync()); } catch (exception ex) { errorhandler.handleerrorasync(ex); } } private async task getinstitutes() { try { dynamiccontainer.setvalue(usercontainerkeyhelper.institutes, await commonrepository.getinstitutesasync()); } catch (exception ex) { errorhandler.handleerrorasync(ex); } }
while debugging i've seen methods executed on mainthread...wasn't supposed on workerthread?
in case i've first task loaded second no parallelism...how can optimize this?
you want concurrency, not parallelism. instead of awaiting each task sequentially, can await on them both using task.whenall
:
public async task loadasync() { isbusy = true; var firstdropdownitemstask = repository.loadstates(); var seconddropdownitemstask = repository.loadinstitutes(); await task.whenall(dropdownitems1task, dropdownitems2task); dropdownitems1 = firstdropdownitemstask.result; dropdownitems2 = seconddropdownitemstask.result; isbusy = false; }
isbusy property that's bound via convention busy indicator how can set ?
usually, items bound via convention need implement inotifypropertychanged
in order update xaml binding value has been updated.
Comments
Post a Comment