async-await

When does a C# Task actually start?

耗尽温柔 提交于 2019-12-17 15:51:11
问题 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

Brief explanation of Async/Await in .Net 4.5

谁说我不能喝 提交于 2019-12-17 15:42:08
问题 How does Asynchronous tasks (Async/Await) work in .Net 4.5? Some sample code: private async Task<bool> TestFunction() { var x = await DoesSomethingExists(); var y = await DoesSomethingElseExists(); return y; } Does the second await statement get executed right away or after the first await returns? 回答1: await pauses the method until the operation completes. So the second await would get executed after the first await returns. For more information, see my async / await intro or the official

Using async without await in C#?

强颜欢笑 提交于 2019-12-17 15:39:39
问题 Consider Using async without await. 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 asynchronous. If you call it, all the code inside the method will execute synchronously. I want write a method that should run async but don't need use await.for example when use a thread public async Task PushCallAsync(CallNotificationInfo callNotificationInfo) { Logger.LogInfo("Pushing new

Combine awaitables like Promise.all

落花浮王杯 提交于 2019-12-17 15:38:18
问题 In asynchronous JavaScript, it is easy to run tasks in parallel and wait for all of them to complete using Promise.all : async function bar(i) { console.log('started', i); await delay(1000); console.log('finished', i); } async function foo() { await Promise.all([bar(1), bar(2)]); } // This works too: async function my_all(promises) { for (let p of promises) await p; } async function foo() { await my_all([bar(1), bar(2), bar(3)]); } I tried to rewrite the latter in python: import asyncio async

Async threadsafe Get from MemoryCache

我与影子孤独终老i 提交于 2019-12-17 15:33:31
问题 I have created a async cache that uses .NET MemoryCache underneath. This is the code public async Task<T> GetAsync(string key, Func<Task<T>> populator, TimeSpan expire, object parameters) { if(parameters != null) key += JsonConvert.SerializeObject(parameters); if(!_cache.Contains(key)) { var data = await populator(); lock(_cache) { if(!_cache.Contains(key)) //Check again but locked this time _cache.Add(key, data, DateTimeOffset.Now.Add(expire)); } } return (T)_cache.Get(key); } I think the

Any disadvantage of using ExecuteReaderAsync from C# AsyncCTP

江枫思渺然 提交于 2019-12-17 15:25:15
问题 There are some articles which indicate that async database calls are bad idea in .NET. Should my database calls be Asynchronous? Should my database calls be Asynchronous Part II On C# Async CTP, there is a System.Data.SqlClient.SqlCommand extension called ExecuteReaderAsync . I have some operations as below on my existing code: var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["hubConnectionString"].ConnectionString; using (var conn = new SqlConnection

In JavaScript, does using await inside a loop block the loop?

走远了吗. 提交于 2019-12-17 15:23:09
问题 Take the following loop: for(var i=0; i<100; ++i){ let result = await some_slow_async_function(); do_something_with_result(); } Does await block the loop? Or does the i continue to be incremented while await ing? Is the order of do_something_with_result() guaranteed sequential with regard to i ? Or does it depend on how fast the await ed function is for each i ? 回答1: Does await block the loop? Or does the i continue to be incremented while await ing? "Block" is not the right word, but yes, i

Deadlock using async & await

耗尽温柔 提交于 2019-12-17 14:56:30
问题 I want my program to follow this callstack/workflow: dispatch() authorize() httpPost() My idea was that httpPost() will be async, while the other 2 methods remain non-async. However, for some reason, it would not work unless I made 2 and 3 async. Maybe I still have some misunderstandings. To my understanding I can either: use the await keyword when calling the async method (this will suspend the method and continue after the async method completes), or omit the await keyword and instead call

Using async keyword in method signature to return a Task in Web Api endpoint

南楼画角 提交于 2019-12-17 14:47:54
问题 If I wanted to write a non-blocking web api action by returning a Task object, I could do it with or without using the async keyword as such: Using async public async Task<HttpResponseMessage> Get() { Func<HttpResponseMessage> slowCall = () => { Thread.Sleep(2000); return Request.CreateResponse(HttpStatusCode.OK, "Hello world"); }; var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall); return await task; } Without using async public Task<HttpResponseMessage> Get() { Func

Using async keyword in method signature to return a Task in Web Api endpoint

十年热恋 提交于 2019-12-17 14:46:03
问题 If I wanted to write a non-blocking web api action by returning a Task object, I could do it with or without using the async keyword as such: Using async public async Task<HttpResponseMessage> Get() { Func<HttpResponseMessage> slowCall = () => { Thread.Sleep(2000); return Request.CreateResponse(HttpStatusCode.OK, "Hello world"); }; var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall); return await task; } Without using async public Task<HttpResponseMessage> Get() { Func