async-ctp

Async Task.WhenAll with timeout

喜欢而已 提交于 2019-11-26 09:37:06
问题 Is there a way in the new async dotnet 4.5 library to set a timeout on the Task.WhenAll method. I want to fetch several sources and stop after say 5 seconds and skip the sources that weren\'t finished. 回答1: You could combine the resulting Task with a Task.Delay() using Task.WhenAny() : await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(timeout)); If you want to harvest completed tasks in case of a timeout: var completedResults = tasks .Where(t => t.Status == TaskStatus.RanToCompletion)

What's the difference between returning void and returning a Task?

别等时光非礼了梦想. 提交于 2019-11-26 08:42:45
问题 In looking at various C# Async CTP samples I see some async functions that return void , and others that return the non-generic Task . I can see why returning a Task<MyType> is useful to return data to the caller when the async operation completes, but the functions that I\'ve seen that have a return type of Task never return any data. Why not return void ? 回答1: SLaks and Killercam's answers are good; I thought I'd just add a bit more context. Your first question is essentially about what

Is Async await keyword equivalent to a ContinueWith lambda?

坚强是说给别人听的谎言 提交于 2019-11-26 04:39:56
问题 Could someone please be kind enough to confirm if I have understood the Async await keyword correctly? (Using version 3 of the CTP) Thus far I have worked out that inserting the await keyword prior to a method call essentially does 2 things, A. It creates an immediate return and B. It creates a \"continuation\" that is invoked upon the completion of the async method invocation. In any case the continuation is the remainder of the code block for the method. So what I am wondering is, are these

How to call an async method from a getter or setter?

主宰稳场 提交于 2019-11-26 01:13:57
问题 What\'d be the most elegant way to call an async method from a getter or setter in C#? Here\'s some pseudo-code to help explain myself. async Task<IEnumerable> MyAsyncMethod() { return await DoSomethingAsync(); } public IEnumerable MyList { get { //call MyAsyncMethod() here } } 回答1: There is no technical reason that async properties are not allowed in C#. It was a purposeful design decision, because "asynchronous properties" is an oxymoron. Properties should return current values; they should

How to limit the amount of concurrent async I/O operations?

偶尔善良 提交于 2019-11-25 23:34:50
问题 // let\'s say there is a list of 1000+ URLs string[] urls = { \"http://google.com\", \"http://yahoo.com\", ... }; // now let\'s send HTTP requests to each of these URLs in parallel urls.AsParallel().ForAll(async (url) => { var client = new HttpClient(); var html = await client.GetStringAsync(url); }); Here is the problem, it starts 1000+ simultaneous web requests. Is there an easy way to limit the concurrent amount of these async http requests? So that no more than 20 web pages are downloaded