For below code I am getting warning for client saying captured variable is disposed in the outer scope, what’s is meaning for this and how can I get rid of it?
using (var client = DeviceClient.CreateFromConnectionString(""))
{
//Loop through each batch in the chunked list
var concurrentTasks = list.Select(r=>
Task.Run(async () =>
{
await Push(r, client);
})).ToList();
//complete all the tasks.
await Task.WhenAll(concurrentTasks);
}
private async Task Push(List<R> r, DeviceClient client)
{
await client.SendEventAsync(new Message(e));
}
>Solution :
I think the inner code is wrong anyhow. You’re not in a synchronous scope, so just write
await Task.WhenAll(
list.Select(r => Push(r, client)).ToList());
No need for all the separate Task.Runs because they just wrap a Task in Task
