async-await

C#. What happens if “after await” thread is busy?

Deadly 提交于 2020-12-31 10:54:54
问题 What happens in C# when awaitable task is finished but the thread in which async method had been started is unavailable (handles another request for example) ? Will then be used another thread instead of the first one, or execution will wait until the busy thread is available ? Thanks in advance for your answers. 回答1: That depends on the SynchronizationContext of the thread on which the continuation was scheduled. For example, when you're using async/await in an app with a UI thread, like an

C#. What happens if “after await” thread is busy?

隐身守侯 提交于 2020-12-31 10:54:31
问题 What happens in C# when awaitable task is finished but the thread in which async method had been started is unavailable (handles another request for example) ? Will then be used another thread instead of the first one, or execution will wait until the busy thread is available ? Thanks in advance for your answers. 回答1: That depends on the SynchronizationContext of the thread on which the continuation was scheduled. For example, when you're using async/await in an app with a UI thread, like an

C#. What happens if “after await” thread is busy?

余生长醉 提交于 2020-12-31 10:54:27
问题 What happens in C# when awaitable task is finished but the thread in which async method had been started is unavailable (handles another request for example) ? Will then be used another thread instead of the first one, or execution will wait until the busy thread is available ? Thanks in advance for your answers. 回答1: That depends on the SynchronizationContext of the thread on which the continuation was scheduled. For example, when you're using async/await in an app with a UI thread, like an

C#. What happens if “after await” thread is busy?

ぐ巨炮叔叔 提交于 2020-12-31 10:54:12
问题 What happens in C# when awaitable task is finished but the thread in which async method had been started is unavailable (handles another request for example) ? Will then be used another thread instead of the first one, or execution will wait until the busy thread is available ? Thanks in advance for your answers. 回答1: That depends on the SynchronizationContext of the thread on which the continuation was scheduled. For example, when you're using async/await in an app with a UI thread, like an

Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? [duplicate]

爷,独闯天下 提交于 2020-12-31 05:49:15
问题 This question already has answers here : ConfigureAwait(true) not returning on the context it was awaited on (2 answers) Closed last month . I was expecting to get back to Thread#1 at location 1.2, but I did not. Is there a way to get back to UI thread after making the async call? Thanks Also I cannot make the top level method async. Not sure if async all the way will solve this issue but I don't have that choice right now. class Program { static void Main(string[] args) { ComputeThenUpdateUI

Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? [duplicate]

做~自己de王妃 提交于 2020-12-31 05:46:21
问题 This question already has answers here : ConfigureAwait(true) not returning on the context it was awaited on (2 answers) Closed last month . I was expecting to get back to Thread#1 at location 1.2, but I did not. Is there a way to get back to UI thread after making the async call? Thanks Also I cannot make the top level method async. Not sure if async all the way will solve this issue but I don't have that choice right now. class Program { static void Main(string[] args) { ComputeThenUpdateUI

Is ConfigureAwait(true) always get back to the orignial thread, even when the callee method does ConfigureAwait(false) internally? [duplicate]

馋奶兔 提交于 2020-12-31 05:45:27
问题 This question already has answers here : ConfigureAwait(true) not returning on the context it was awaited on (2 answers) Closed last month . I was expecting to get back to Thread#1 at location 1.2, but I did not. Is there a way to get back to UI thread after making the async call? Thanks Also I cannot make the top level method async. Not sure if async all the way will solve this issue but I don't have that choice right now. class Program { static void Main(string[] args) { ComputeThenUpdateUI

What is the right way to cancel all async/await tasks within an useEffect hook to prevent memory leaks in react?

帅比萌擦擦* 提交于 2020-12-30 08:40:11
问题 I am working on a react chap app that pulls data from a firebase database. In my "Dashboard" component I have an useEffect hook checking for an authenticated user and if so, pull data from firebase and set the state of a an email variable and chats variable. I use abortController for my useEffect cleanup, however whenever I first log out and log back in I get a memory leak warning. index.js:1375 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it

When unit testing, how do I mock a return null from async method?

非 Y 不嫁゛ 提交于 2020-12-29 08:59:30
问题 Normally, I mock my repo like so: var repository = new Mock<ISRepository>(); repository.Setup(r => r.GetMemberAsync(email)) .Returns(Task.FromResult(new Member { FirstName = firstName, LastName = lastName })); But, in my code, I check to see if the member is not found, i.e. GetMemberAsync returns null. How do I mock this? I tried: var repository = new Mock<ISRepository>(); repository.Setup(r => r.GetMemberAsync(email)) .Returns(Task.FromResult<object>(null)); but I get a compile error. 回答1:

How to loop async/await?

╄→尐↘猪︶ㄣ 提交于 2020-12-29 07:40:45
问题 I need to repeat async/await block several times, but cannot use the following code: for (let i = 0; i <= 10; i += 1) { const res = await DoSomething(); } because it contradicts with no-await-in-loop rule. 回答1: Use Promise.all if order of iteration doesn't matter If you don't mind code running out-of-order (meaning order of each iteration doesn't matter), just use Promise.all instead: const promises = []; for (let i = 0; i <= 10; i += 1) { promises.push(DoSomething()); } const responses =