async-await

How does C# 5.0's async-await feature differ from the TPL?

北慕城南 提交于 2019-12-17 17:25:35
问题 I don't see the different between C#'s (and VB's) new async features, and .NET 4.0's Task Parallel Library. Take, for example, Eric Lippert's code from here: async void ArchiveDocuments(List<Url> urls) { Task archive = null; for(int i = 0; i < urls.Count; ++i) { var document = await FetchAsync(urls[i]); if (archive != null) await archive; archive = ArchiveAsync(document); } } It seems that the await keyword is serving two different purposes. The first occurrence ( FetchAsync ) seems to mean,

warning this call is not awaited, execution of the current method continues

断了今生、忘了曾经 提交于 2019-12-17 17:24:21
问题 Just got VS2012 and trying to get a handle on async . Let's say I've got an method that fetches some value from a blocking source. I don't want caller of the method to block. I could write the method to take a callback which is invoked when the value arrives, but since I'm using C# 5, I decide to make the method async so callers don't have to deal with callbacks: // contrived example (edited in response to Servy's comment) public static Task<string> PromptForStringAsync(string prompt) {

async await and threading

…衆ロ難τιáo~ 提交于 2019-12-17 16:59:23
问题 based on what I have read on MSDN The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available. It

Am I right to ignore the compiler warning for lacking await for this async call?

依然范特西╮ 提交于 2019-12-17 16:42:15
问题 I have the following method that is triggered when an exception occurs in a part of my Metro application void Model_ExceptionOccured(Exception ex) { var dlg = new Windows.UI.Popups.MessageDialog("An exception occured during verification: " + ex.Message, "Exception"); dlg.ShowAsync(); } The 'dlg.ShowAsync()'-call is asynchronous, but I don't care to wait for the result. The compiler generates a warning for it though: Because this call is not awaited, execution of the current method continues

Proper request with async/await in Node.JS

霸气de小男生 提交于 2019-12-17 16:32:11
问题 In my program I make async call for my function from another API module: var info = await api.MyRequest(value); Module code: var request = require("request") module.exports.MyRequest = async function MyRequest(value) { var options = { uri: "http://some_url", method: "GET", qs: { // Query string like ?key=value&... key : value }, json: true } try { var result = await request(options); return result; } catch (err) { console.error(err); } } Execution returns immediately, however result and

How to implement interface method that returns Task<T>?

旧城冷巷雨未停 提交于 2019-12-17 16:27:02
问题 I have an interface interface IFoo { Task<Bar> CreateBarAsync(); } There are two methods to create Bar , one asynchronous and one synchronous. I want to provide an interface implementation for each of these two methods. For the asynchronous method, the implementation could look like this: class Foo1 : IFoo { async Task<Bar> CreateBarAsync() { return await AsynchronousBarCreatorAsync(); } } But HOW should I implement the class Foo2 that uses the synchronous method to create Bar ? I could

What “Current” properties flow with ExecutionContext

好久不见. 提交于 2019-12-17 16:23:07
问题 This is a two part question: Can someone provide a list of the ASP.NET/.NET properties that are typically thread local that flow with ExecutionContext? HttpContext.Current? Thread.CurrentContext? Thread.CurrentPrincipal? Thread.CurrentCulture? What properties can I count on surviving/persisting async/await? What else? Is there any way to add application specific Context information that will flow automatically with ExecutionContext? Something like var ec = ExecutionContext.Capture(); ec

MoveNext instead of actual method/task name

喜欢而已 提交于 2019-12-17 16:04:23
问题 Using log4net declared as: private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType()); In an async method or task, like this one: public async void CheckSomething() { log.Info(null); //.... } logs MoveNext instead of CheckSomething . Any idea how to make it log an actual method name? 回答1: All async methods are rewritten into a state machine to satisfy potential await values within the method. The final method in which the code lives is the MoveNext method

async/await keywords not available in .net 4.0

≯℡__Kan透↙ 提交于 2019-12-17 15:54:39
问题 I would like to use the async/await in C# 4.0 and I have installed the following package: http://www.nuget.org/packages/Microsoft.Bcl.Async/ The problem is that I do not have async/await keywords available. I am using VS2010 with SP1. Thanks. 回答1: You're not going to get a better answer than Jon Skeet's. The only supported way to do this is to use VS2012 with Microsoft.Bcl.Async . VS2010 is very difficult to get working with async / await . There was an old Async CTP package (which had many

When does a C# Task actually start?

↘锁芯ラ 提交于 2019-12-17 15:51:48
问题 When does a Task actually start? public void DoSomething() { Task myTask = DoSomethingAsync(); Task.WaitAll(new[] { myTask }, 2000); } public async Task DoSomethingAsync() { await SomethingElse(); } Does it start immediately when initializing it in Task myTask = DoSomethingAsync(); or does it start when you say to wait for it in Task.WaitAll(new[] { myTask }, 2000); ? 回答1: Calling an async method returns a hot task, a task that has already been started. So there is no actual code necessary to