Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get rid of the warning "captured variable is disposed in the outer scope"

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?

enter image description here

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));
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading