Randy
Randy

Reputation: 1287

Setting Up an Async Task

Would someone point me in the right Direction Setting Up an Async Task? I have a InitializeDatabases Method with the following Task:

    Task.Run(async () => 
        {
            await NContext.ZSA_TransactionHeader.LoadAsync();
            await NContext.ZSA_TransactionDetail.LoadAsync();
            await DContext.AR_MasterTable.LoadAsync();
            await DContext.IN_Master.LoadAsync();
            await DContext.SA_HistoryHeader.LoadAsync();
            await DContext.SA_HistoryDetail.LoadAsync();
            await DContext.SA_NonInvCode.LoadAsync();
            await DContext.SA_SalespCode.LoadAsync();
        }).Wait();

My problem is that the task executes but continues even when it has not finished loading. I need it to wait until all the .LoadAsync tasks are complete. I would also like all the LoadAsync tasks to run concurrently. I have gathered this much from other SO questions but I feel like I am in over my head with this.

Upvotes: 0

Views: 80

Answers (1)

SLaks
SLaks

Reputation: 887195

By awaiting each call, you're waiting for each one to finish before starting the next one.

By calling .Wait() at the end, you're freezing your UI anyway.

You want await Task.WhenAll(x.LoadAsync(), y.LoadAsync(), ...).

Upvotes: 3

Related Questions