async-await

Regarding the usage of SemaphoreSlim with Async/Await

[亡魂溺海] 提交于 2020-01-23 06:48:13
问题 I am not an advanced developer. I'm just trying to get a hold on the task library and just googling. I've never used the class SemaphoreSlim so I would like to know what it does. Here I present code where SemaphoreSlim is used with async & await but which I do not understand. Could someone help me to understand the code below. 1st set of code await WorkerMainAsync(); async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); while (true) { await ss.WaitAsync(); // you should

Regarding the usage of SemaphoreSlim with Async/Await

左心房为你撑大大i 提交于 2020-01-23 06:47:32
问题 I am not an advanced developer. I'm just trying to get a hold on the task library and just googling. I've never used the class SemaphoreSlim so I would like to know what it does. Here I present code where SemaphoreSlim is used with async & await but which I do not understand. Could someone help me to understand the code below. 1st set of code await WorkerMainAsync(); async Task WorkerMainAsync() { SemaphoreSlim ss = new SemaphoreSlim(10); while (true) { await ss.WaitAsync(); // you should

Value is not updating after async method resume

依然范特西╮ 提交于 2020-01-23 05:52:23
问题 Looking at this code : public class SharedData { public int Value { get; set; } } void button1_Click(object sender, EventArgs e) { AAA(); } async Task BBB(SharedData data) { await Task.Delay(TimeSpan.FromSeconds(1)); MessageBox.Show(data.Value.ToString()); //<---- I always see 0 here, data.Value = data.Value + 1; } async Task<int> AAA() { SharedData data = new SharedData(); var task1 = BBB(data); var task2 = BBB(data); var task3 = BBB(data); await Task.WhenAll(task1, task2, task3); MessageBox

FluentAssertions ShouldNotThrow is not recognised for an async method/Func

爱⌒轻易说出口 提交于 2020-01-23 04:49:28
问题 I am trying to check an async method throws concrete exception. For that I am using MSTEST and FluentAssertions 2.0.1. I have checked this Discussion on Codeplex and to see how it works with async-exception methods this another one link about FluentAssertions async tests: After a while trying to work with my 'production' code I have switched off to the Fluentassertions fake aync class and my resulting code is like this (put this code inside a [TestClass] : [TestMethod] public void

Many awaits for Async method, or a single await for a wrapping Task.Run?

China☆狼群 提交于 2020-01-23 03:03:05
问题 Suppose we have to write down on database a list of 1000 elements, through an async flow. Is it better to await 1000 times an asynchronous insert statement, or to wrap all the 1000 inserts in one single synchronous method encapsulated into a Task.Run statement, awaiting one single time? For example, SqlCommand has every method coupled with his async version. In this case, we have an insert statement, so we can call ExecuteNonQuery or ExecuteNonQueryAsync . Often, on async/await guidelines, we

Cannot access a disposed object for DbContext in .NET Core for Async method

别说谁变了你拦得住时间么 提交于 2020-01-23 02:59:12
问题 I am having a weird problem in one of my microservice web api. My async GET methods throw a Cannot access a disposed object exception for my DbContext except for the very first time they are invoked. I tried looking online for an answer but nothing worked. I made sure my methods are not async void and I await the necessary calls. Since my POST and DELETE methods work fine, I am fairly certain that the real culprit is the IMapper instance. I think it might always point to the first instance of

How does async-await “save threads”?

南笙酒味 提交于 2020-01-23 01:38:05
问题 I understand that with threadless async there are more threads available to service inputs (e.g. a HTTP request), but I don't understand how that doesn't potentially cause cause thread starvation when the async operations complete and a thread is needed to run their continuation. Let's say we only have 3 threads Thread 1 | Thread 2 | Thread 3 | and they get blocked on long-running operations that require threads (e.g. make database query on separate db server) Thread 1 | --- | Start servicing

Is there a way to run async lambda synchronously?

三世轮回 提交于 2020-01-22 19:47:10
问题 Is there a way to run async lambda synchronously? It is not allowed to modify lambda expression, it must be leaved as is. Copy/paste example(it's an abstraction): var loopCnt = 1000000; Action<List<int>> aslambda = async (l) => { Thread.Sleep(100); await Task.Run(() => { }); for (int i = 0; i < loopCnt; i++) { l.Add(i); } }; var lst = new List<int>(); aslambda(lst); //This runs asynchronously if (lst.Count < loopCnt-100) { ; } Solution: It is really a dilemma for me to accept one answer. I

Why use C# async/await for CPU-bound tasks [closed]

十年热恋 提交于 2020-01-22 19:38:24
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . I'm getting the hang of the async/await keywords in C#, and how they facilitate asynchronous programming - allowing the the thread to be used elsewhere whilst some I/O bound task like a db call is going on. I have read numerous times that async/await is for I/O bound tasks,

Using axios with async and await

女生的网名这么多〃 提交于 2020-01-22 16:02:49
问题 I am new in Async and await ecosystem, but I know that it gives the way of coding in a synchronous way (although it is async behind the scenes, just the way it is written in code). So here is my code that I want to do in an async fashion. const axios = require("axios"); async function getJSONAsync(){ // The await keyword saves us from having to write a .then() block. let json = await axios.get('https://tutorialzine.com/misc/files/example.json'); console.log('after the call to service'); //