async-await

async and await are single threaded Really?

好久不见. 提交于 2020-02-17 14:04:49
问题 I created following code: using System; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main() { Console.WriteLine("M Start"); MyMethodAsync(); Console.WriteLine("M end"); Console.Read(); } static async Task MyMethodAsync() { await Task.Yield(); Task<int> longRunningTask = LongRunningOperationAsync(); Console.WriteLine("M3"); //and now we call await on the task int result = await longRunningTask; //use the result Console.WriteLine(result); } static

Async function with +=

*爱你&永不变心* 提交于 2020-02-12 08:07:25
问题 let x = 0; async function test() { x += await 5; console.log('x :', x); } test(); x += 1; console.log('x :', x); The values of x logged are 1 and 5 . My question is: why is the value of x 5 on second log? If the test is executed after x += 1 (since it is an async function) then the value of x is 1 by the time test is executed, so x += await 5 should make the value of x 6 . 回答1: TL;DR: Because += reads x before, but writes it after it has changed, due to await . async functions run

How to throttle multiple asynchronous tasks?

旧街凉风 提交于 2020-02-10 03:18:16
问题 I have some code of the following form: static async Task DoSomething(int n) { ... } static void RunThreads(int totalThreads, int throttle) { var tasks = new List<Task>(); for (var n = 0; n < totalThreads; n++) { var task = DoSomething(n); tasks.Add(task); } Task.WhenAll(tasks).Wait(); // all threads must complete } Trouble is, if I don't throttle the threads, things start falling apart. Now, I want to launch a maximum of throttle threads, and only start the new thread when an old one is

How to throttle multiple asynchronous tasks?

时光怂恿深爱的人放手 提交于 2020-02-10 03:18:08
问题 I have some code of the following form: static async Task DoSomething(int n) { ... } static void RunThreads(int totalThreads, int throttle) { var tasks = new List<Task>(); for (var n = 0; n < totalThreads; n++) { var task = DoSomething(n); tasks.Add(task); } Task.WhenAll(tasks).Wait(); // all threads must complete } Trouble is, if I don't throttle the threads, things start falling apart. Now, I want to launch a maximum of throttle threads, and only start the new thread when an old one is

How do I ThreadStart a method that is labelled async?

半腔热情 提交于 2020-02-08 10:44:35
问题 I want to run a processing loop on a separate thread: _processingThread = new Thread(new ThreadStart(DoWork))); But DoWork needs to be async: private async Task QueueProcessorDoWork() { while (true) { await something(); } } How can I connect the two together? When I add async Task , it doesn't match the parameter of ThreadStart. It is possible to make the method that sets up the thread async Task , I think, but I am not sure if that would help. What's the best solution here? I need my thread

Iterate over CSV file and run await for each row?

旧街凉风 提交于 2020-02-07 02:09:12
问题 I want to iterate over a CSV file in node, and for each row, call an asynchronous function and wait for it to complete. How can I do this? I have: const getFile = async function(url) { const response = await page.goto(url, { waitUntil: 'networkidle2' }); await page.waitFor(3000); const ad = await page.waitForSelector('div.chart'); await ad.screenshot({ path: path }); }; fs.createReadStream(fname) .pipe(csv()) .on('data', (row) => { let id = row.ad_id; let url = 'xxxx' + id; await getFile(path

FutureBuilder snapshot Data does not return anything , Why?

不打扰是莪最后的温柔 提交于 2020-02-06 09:53:57
问题 Trying to get stored data by SharedPreferences But does not work .. my main.dart : Widget build(BuildContext context) { return StreamProvider<User>.value( value: AuthService().user, child: FutureBuilder<String>( future: SaadConstants.setValueFromLocal('app_lang','ar'), builder: (BuildContext context, AsyncSnapshot<String> snapshot){ return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( body:Wrapper(), ), ); } ), ); } my wrapper : @override Widget build(BuildContext context) {

FutureBuilder snapshot Data does not return anything , Why?

拈花ヽ惹草 提交于 2020-02-06 09:53:11
问题 Trying to get stored data by SharedPreferences But does not work .. my main.dart : Widget build(BuildContext context) { return StreamProvider<User>.value( value: AuthService().user, child: FutureBuilder<String>( future: SaadConstants.setValueFromLocal('app_lang','ar'), builder: (BuildContext context, AsyncSnapshot<String> snapshot){ return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( body:Wrapper(), ), ); } ), ); } my wrapper : @override Widget build(BuildContext context) {

StackOverflow exception when overriding a Task returning method when adding async

别来无恙 提交于 2020-02-05 09:14:08
问题 I have a custom user manager class (ASP .NET Identity). I want to override the FindByIdMethod, so that it automatically loads the role names for the user model. This is the only method I am overriding. I am using Microsoft.Identity nuget packages version 2.2.1, Asp.Net Framework. However, the code below throws a StackOverflow exception - at await base.FindByIdAsync(userId); public class MyUserManager : UserManager<MyUser>, IMyUserManager { public override async Task<MyUser> FindByIdAsync

Library method: Async or not?

强颜欢笑 提交于 2020-02-05 02:45:13
问题 I have gotten it through my head (perhaps incorrectly) that library methods that take some time should generally be made async. Is that true, and if so, how should that be done when there is nothing to await within the library method? I am designing my own library with the method: public Dictionary<FunctionEnum, double> Evaluate(Func<uint,uint> algorithm, IList<double> suggestedList) It takes in a method that takes a uint and returns a uint and uses that method many times over. In brief, I'm