In a wpf application made of caliburn micro and telerik controls I've different screens that load data from a remote service then show data in a gridview /fills comboboxes. I'm using a sync/await operators to make the load retrieval operations but I'm almost certain it has some bottleneck. Doing await I have the main UI thread to wait syncronization with the worker thread... Consider this 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 that case I've first task loaded then second with no parallelism...how can I optimize this? About my IsBusy property that's bound via convention to a busy indicator how can it be properly set ? thanks
Update #1:
I'n my real code I do
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 are 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 that all the methods are executed on the MainThread...wasn't supposed to be on workerthread?
System.Threading.Tasks.Task.WhenAll()method. TheIsBusyassignment looks okay, but assuming your ViewModel implementsINotifyPropertyChanged, you'd need something like anonPropertyChanged("IsBusy")each time when you set the property content in order to notify the XAML "View"IsBusy=True; try{....} finally {IsBusy=False;}