1

I'm trying to run multiple Task using async but when execution reaches WhenAll() this method call never returns.

 List<Task> tasks = new(); tasks.Add(Task.Run( async () => viewModel.Customers = await CustomerService.GetAllAsync())); tasks.Add(Task.Run( async () => viewModel.Orders = await OrderService.GetAllAsync())); await Task.WhenAll(tasks.ToArray()); 

EDIT:

I was able to debug execution all the way into CircuitHost.cs, and on line 125 it stops there.

enter image description here

Here's the stack trace at line 125:

async CircuitHost.<>c__DisplayClass36_0.<InitializeAsync>b__0()at C:\Users\David\AppData\Roaming\JetBrains\Rider2022.1\resharper-host\SourcesCache\d848ab5bf79df2599514458b1cacef69f49ae55787993acba112f74d5315a7\CircuitHost.cs:line 125 AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost.<>c__DisplayClass36_0.<<InitializeAsync>b__0>d>() AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost.<>c__DisplayClass36_0.<<InitializeAsync>b__0>d>() CircuitHost.<>c__DisplayClass36_0.<InitializeAsync>b__0() RendererSynchronizationContext.<>c.<<InvokeAsync>b__9_0>d.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__9_0>d>() AsyncVoidMethodBuilder.Start<Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__9_0>d>() RendererSynchronizationContext.<>c.<InvokeAsync>b__9_0() RendererSynchronizationContext.ExecuteSynchronously() RendererSynchronizationContext.ExecuteSynchronouslyIfPossible() RendererSynchronizationContext.InvokeAsync() RendererSynchronizationContextDispatcher.InvokeAsync() CircuitHost.InitializeAsync()at C:\Users\David\AppData\Roaming\JetBrains\Rider2022.1\resharper-host\SourcesCache\d848ab5bf79df2599514458b1cacef69f49ae55787993acba112f74d5315a7\CircuitHost.cs:line 100 ComponentHub.<StartCircuit>d__13.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.Components.Server.ComponentHub.<StartCircuit>d__13>() AsyncValueTaskMethodBuilder<string>.Start<Microsoft.AspNetCore.Components.Server.ComponentHub.<StartCircuit>d__13>() ComponentHub.StartCircuit() [Lightweight Method Call] DefaultHubDispatcher<ComponentHub>.<ExecuteMethod>d__23.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<ExecuteMethod>d__23>() AsyncValueTaskMethodBuilder<object>.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<ExecuteMethod>d__23>() DefaultHubDispatcher<ComponentHub>.ExecuteMethod() DefaultHubDispatcher<ComponentHub>.ExecuteHubMethod() DefaultHubDispatcher<ComponentHub>.<<Invoke>g__ExecuteInvocation|18_0>d.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<<Invoke>g__ExecuteInvocation|18_0>d>() AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<<Invoke>g__ExecuteInvocation|18_0>d>() DefaultHubDispatcher<ComponentHub>.<Invoke>g__ExecuteInvocation|18_0() DefaultHubDispatcher<ComponentHub>.<Invoke>d__18.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<Invoke>d__18>() AsyncTaskMethodBuilder<bool>.Start<Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>.<Invoke>d__18>() DefaultHubDispatcher<ComponentHub>.Invoke() DefaultHubDispatcher<__Canon>.<>c.<ProcessInvocation>b__17_0() ChannelBasedSemaphore.<RunTask>d__7<(DefaultHubDispatcher<ComponentHub>, HubMethodDescriptor, HubConnectionContext, HubMethodInvocationMessage)>.MoveNext() AsyncMethodBuilderCore.Start<Microsoft.AspNetCore.SignalR.Internal.ChannelBasedSemaphore.<RunTask>d__7<(Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>, Microsoft.AspNetCore.SignalR.Internal.HubMethodDescriptor, Microsoft.AspNetCore.SignalR.HubConnectionContext, Microsoft.AspNetCore.SignalR.Protocol.HubMethodInvocationMessage)>>() AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.SignalR.Internal.ChannelBasedSemaphore.<RunTask>d__7<(Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>, Microsoft.AspNetCore.SignalR.Internal.HubMethodDescriptor, Microsoft.AspNetCore.SignalR.HubConnectionContext, Microsoft.AspNetCore.SignalR.Protocol.HubMethodInvocationMessage)>>() ChannelBasedSemaphore.RunTask<(Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>, Microsoft.AspNetCore.SignalR.Internal.HubMethodDescriptor, Microsoft.AspNetCore.SignalR.HubConnectionContext, Microsoft.AspNetCore.SignalR.Protocol.HubMethodInvocationMessage)>() ChannelBasedSemaphore.RunAsync<(Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher<Microsoft.AspNetCore.Components.Server.ComponentHub>, Microsoft.AspNetCore.SignalR.Internal.HubMethodDescriptor, Microsoft.AspNetCore.SignalR.HubConnectionContext, Microsoft.AspNetCore.SignalR.Protocol.HubMethodInvocationMessage)>() DefaultHubDispatcher<ComponentHub>.ProcessInvocation() DefaultHubDispatcher<__Canon>.DispatchMessageAsync() HubConnectionHandler<ComponentHub>.<DispatchMessagesAsync>d__20.MoveNext() AsyncTaskMethodBuilder<VoidTaskResult>.AsyncStateMachineBox<HubConnectionHandler<ComponentHub>.<DispatchMessagesAsync>d__20>.ExecutionContextCallback() ExecutionContext.RunFromThreadPoolDispatchLoop() AsyncTaskMethodBuilder<VoidTaskResult>.AsyncStateMachineBox<HubConnectionHandler<ComponentHub>.<DispatchMessagesAsync>d__20>.MoveNext() AsyncTaskMethodBuilder<VoidTaskResult>.AsyncStateMachineBox<HubConnectionHandler<ComponentHub>.<DispatchMessagesAsync>d__20>.ExecuteFromThreadPool() ThreadPoolWorkQueue.Dispatch() PortableThreadPool.WorkerThread.WorkerThreadStart() Thread.StartCallback() [Native to Managed Transition] 
7
  • Do you mean that that task never completes? Commented Jan 9, 2023 at 18:34
  • yes, WhenAll() doesn't complete Commented Jan 9, 2023 at 18:37
  • 1
    what does the code up the call stack look like? Commented Jan 9, 2023 at 18:41
  • Could you include also the setter of the viewModel.Customers property, and the CustomerService.GetAllAsync() method? I assume that the Orders-related counterpart is similar. You could test this assumption by commenting out the Orders-related part, and see if the problem persists with a single task in the tasks list. Commented Jan 9, 2023 at 19:36
  • 1
    Could you try replacing the Task.Run with this custom Run method: static async Task Run(Func<Task> action) => await action();, and see if it makes any difference? Commented Jan 9, 2023 at 19:44

1 Answer 1

2

Try to re-organize your code like this:

var customersRetrieval = CustomerService.GetAllAsync(); var ordersRetrieval = OrderService.GetAllAsync(); await Task.WhenAll(customersRetrieval, ordersRetrieval); viewModel.Customers = await customersRetrieval; viewModel.Orders = await ordersRetrieval; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.