c#-5.0

Using async without await

十年热恋 提交于 2019-11-27 04:44:57
I'd like to make a function async, so I simply add async like this: public async static void something(){ } You can see that its return-type is void . I just want this function to be called asynchronously without blocking, since return is void so no await is needed. But Visual Studio 2012 just cannot compile this, it says that I miss await ? Could you please advise a sample that makes a function async without using await . I think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be

Can't await async extension method

南笙酒味 提交于 2019-11-27 04:43:11
问题 Situation is pretty simple - I wrote an extension method and made it async with return type Task<T> . But when I try to call it using await, compiler throws an error which suggests that the extension method wasn't recognized as async at all. Here's my code: public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class { var node = client.Create(source); var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/

How to await a method in a Linq query

别来无恙 提交于 2019-11-27 03:52:59
问题 Trying to use the await keyword in a LINQ query and I get this: The 'await' operator may only be used in a query expression within the first collection expression of the initial 'from' clause or within the collection expression of a 'join' clause Sample Code: var data = (from id in ids let d = await LoadDataAsync(id) select d); Is it not possible to await something in a LINQ query, or does it need to be structured a different way? 回答1: LINQ has very limited support for async / await . For

How to yield from parallel tasks in .NET 4.5

半世苍凉 提交于 2019-11-27 03:25:19
问题 I would like to use .NET iterator with parallel Tasks/await?. Something like this: IEnumerable<TDst> Foo<TSrc, TDest>(IEnumerable<TSrc> source) { Parallel.ForEach( source, s=> { // Ordering is NOT important // items can be yielded as soon as they are done yield return ExecuteOrDownloadSomething(s); } } Unfortunately .NET cannot natively handle this. Best answer so far by @svick - use AsParallel(). BONUS: Any simple async/await code that implements multiple publishers and a single subscriber?

Difference between the TPL & async/await (Thread handling)

对着背影说爱祢 提交于 2019-11-27 02:45:16
Trying to understanding the difference between the TPL & async / await when it comes to thread creation. I believe the TPL ( TaskFactory.StartNew ) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool. That's of course unless you use TaskCreationOptions.LongRunning which creates a new thread. I thought async / await would work similarly so essentially: TPL: Factory.StartNew( () => DoSomeAsyncWork() ) .ContinueWith( (antecedent) => { DoSomeWorkAfter(); },TaskScheduler.FromCurrentSynchronizationContext()); Async / Await : await DoSomeAsyncWork();

Why can't I catch an exception from async code?

ぃ、小莉子 提交于 2019-11-27 01:42:24
Everywhere I read it says the following code should work, but it doesn't. public async Task DoSomething(int x) { try { // Asynchronous implementation. await Task.Run(() => { throw new Exception(); x++; }); } catch (Exception ex) { // Handle exceptions ? } } That said, I'm not catching anything and get an "unhandled exception" originating at the 'throw' line. I'm clueless here. You have the "Just my code" Option turned on. With this on, it is considering the exception unhandled with respect to "just your code"--because other code is catching the exception and stuffing it inside of a Task, later

Using ASP.NET Web API, my ExecutionContext isn't flowing in async actions

若如初见. 提交于 2019-11-27 01:19:54
问题 I'm having difficulty understanding the mechanics behind ExecutionContext. From what I've read online, context-sensitive items such as security (Thread Principal), culture, etc, should flow across asynchronous threads within the bounds of an execution unit of work. I'm encountering very confusing and potentially dangerous bugs though. I'm noticing my thread's CurrentPrincipal is getting lost across async execution. Here is an example ASP.NET Web API scenario: First, let's setup a simple Web

AspNetSynchronizationContext

亡梦爱人 提交于 2019-11-27 01:19:08
问题 Trying to use new C# 5 async model it was surprising to me AspNetSynchronizationContext is an internal class (as well as AspNetSynchronizationContextBase base). Thus undocumented. But it's essential to know what it does when utilizing async/await feature within your ASP.NET code. Am I correct that It does guarantee your continuations will get the same HttpContext.Current as original callers? It does not guarantee the continuations will execute on the same thread as the callers? If the latter

Is async/await suitable for methods that are both IO and CPU bound?

醉酒当歌 提交于 2019-11-27 00:03:12
The MSDN documentation appears to state that async and await are suitable for IO-bound tasks whereas Task.Run should be used for CPU-bound tasks. I'm working on an application that performs HTTP requests to retrieve HTML documents, which it then parses. I have a method that looks like this: public async Task<HtmlDocument> LoadPage(Uri address) { using (var httpResponse = await new HttpClient().GetAsync(address)) //IO-bound using (var responseContent = httpResponse.Content) using (var contentStream = await responseContent.ReadAsStreamAsync()) return await Task.Run(() => LoadHtmlDocument

Do the new C# 5.0 'async' and 'await' keywords use multiple cores?

坚强是说给别人听的谎言 提交于 2019-11-26 23:49:52
Two new keywords added to the C# 5.0 language are async and await , both of which work hand in hand to run a C# method asynchronously without blocking the calling thread. My question is, do these methods actually take advantage of multiple cores and run in parallel or does the async method run in the same thread core as the caller? Two new keywords added to the C# 5.0 language are async and await, both of which work hand in hand to run a C# method asynchronously without blocking the calling thread. That gets across the purpose of the feature, but it gives too much "credit" to the async/await