简体   繁体   中英

Make multiple async calls in a single call in C#

I want to make multiple asynchronous calls in a single call without using a loop. For example, I'd like to pass a list of URIs and and retrieve all of them asynchronously in one call. Is this possible via C#?

Here's an example in Java that does something similar to what I am looking for.

You can get a list of tasks very easily - for example:

var tasks = uris.Select(async uri =>
            {
                using (var client = new HttpClient())
                {
                    return await client.GetStringAsync(uri)
                }
            })
            .ToList();

Note that if you want to use async/await, you'll need C# 5, not C# 4. You can still use .NET 4.0 if you use the Microsoft.Bcl.Async Nuget package, but you must use a C# 5 compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM