async-await

Lazy<Task<T>> with asynchronous initialization

孤街醉人 提交于 2021-02-04 19:40:25
问题 class Laziness { static string cmdText = null; static SqlConnection conn = null; Lazy<Task<Person>> person = new Lazy<Task<Person>>(async () => { using (var cmd = new SqlCommand(cmdText, conn)) using (var reader = await cmd.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { string firstName = reader["first_name"].ToString(); string lastName = reader["last_name"].ToString(); return new Person(firstName, lastName); } } throw new Exception("Failed to fetch Person"); }); public async Task

Behavior of async await with new threads

孤街醉人 提交于 2021-02-04 19:31:38
问题 I am trying to understand the precise behavior of async/await and am having a small amount of trouble wrapping my head around it. Consider this example: public async void StartThread() { while(true){ SomeOtherClass.SomeSynchronousStuff(); var something = await SomeOtherClass.SomeOtherAsyncMethod(); } } public void ConstructorForThisClass() { Thread thread = new Thread(StartThread); thread.Start(); } My understanding of async/await is that what is happening under the covers is that the

Best practice - warning: method lacks 'await' operator warning

↘锁芯ラ 提交于 2021-02-04 19:12:51
问题 Yes, I know there are other questions to cover what this warning means and how to solve it, however, I have a question about best practices regarding async programming. I have a service layer which handles data transfer between the data layer and the presentation layer. This service contains several methods which query the database, and return the result. I have been trying to use async programming wherever possible. An example: public async Task<SiteTemplateResource[]> GetResources(int

How can I call async method from constructor?

空扰寡人 提交于 2021-02-04 17:08:47
问题 I need to call a async method from my Form1 constructor. Since a constructor can't have a return type, I can't add a async void . I read that static constructor can be async but I need to call methods from constructor that aren't static , such as InitializeComponent() (since it's the Form's constructor). The class is: public partial class Form1 : Form { InitializeComponent(); //some stuff await myMethod(); } I read this one too but I still don't know how to implement this (in my case) since

dynamic in Task.Run

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-04 15:51:23
问题 I have a long running task with the same name in unrelated classes. I was trying to have this code in common method using dynamic. I am getting following error Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code Message=Cannot implicitly convert type 'void' to 'object' I tried to isolate the code to the following class Program { static void Main(string[] args) { MainAsync(); Console.ReadKey(); } static async void MainAsync() { var classA = new ClassA(); var classB

Async await - does await block other code from running?

我只是一个虾纸丫 提交于 2021-02-04 06:14:30
问题 In javascript, does await block code? For example, let's say we have the below code: async function queryDB() { const addUser = await promisePool.execute("INSERT INTO Users (User) VALUES ('username')") const selectUser = await promisePool.execute("SELECT User FROM Users") } Will "selectUser" wait to run until addUser is finished so that we can select the user that is added? Also, let's say that we add some code between the awaits that is not a promise, something like this: async function

Async await - does await block other code from running?

喜欢而已 提交于 2021-02-04 06:13:58
问题 In javascript, does await block code? For example, let's say we have the below code: async function queryDB() { const addUser = await promisePool.execute("INSERT INTO Users (User) VALUES ('username')") const selectUser = await promisePool.execute("SELECT User FROM Users") } Will "selectUser" wait to run until addUser is finished so that we can select the user that is added? Also, let's say that we add some code between the awaits that is not a promise, something like this: async function

Running async functions on Google Apps Script

别等时光非礼了梦想. 提交于 2021-02-04 05:18:51
问题 I am attempting to run WebAssembly on the new V8 Google Apps Script runtime, and it appears to be supported, however it seems that async functions are terminated after they return a Promise. let wasm= new Uint8Array([/* snip */]).buffer function add(a,b) { return((async()=>{ console.log("running function...") results=await WebAssembly.instantiate(wasm) return results.instance.exports.add(a,b) })()); } function test(){ add(2,3).then(console.log).catch(console.error) } when I run test "running

Running async functions on Google Apps Script

孤者浪人 提交于 2021-02-04 05:05:35
问题 I am attempting to run WebAssembly on the new V8 Google Apps Script runtime, and it appears to be supported, however it seems that async functions are terminated after they return a Promise. let wasm= new Uint8Array([/* snip */]).buffer function add(a,b) { return((async()=>{ console.log("running function...") results=await WebAssembly.instantiate(wasm) return results.instance.exports.add(a,b) })()); } function test(){ add(2,3).then(console.log).catch(console.error) } when I run test "running

Running async functions on Google Apps Script

落爺英雄遲暮 提交于 2021-02-04 05:04:20
问题 I am attempting to run WebAssembly on the new V8 Google Apps Script runtime, and it appears to be supported, however it seems that async functions are terminated after they return a Promise. let wasm= new Uint8Array([/* snip */]).buffer function add(a,b) { return((async()=>{ console.log("running function...") results=await WebAssembly.instantiate(wasm) return results.instance.exports.add(a,b) })()); } function test(){ add(2,3).then(console.log).catch(console.error) } when I run test "running