async-await

Correct pattern to dispose of cancellation token source

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-02 12:10:16
问题 Consider a scenario where you have some asynchronous work to be done and you can run it in a fire and forget mode. This asynchronous work is able to listen for cancellation and so you pass it a cancellation token in order to being able to cancel it. At a given moment in time we can decide to request the cancellation of the ongoing activity, by using the cancellation token source object from which we have taken the cancellation token. Because cancellation token source implements IDisposable ,

Await an async function from setter property

£可爱£侵袭症+ 提交于 2021-01-02 07:08:45
问题 I need to await to an async function from a property setter method. public String testFunc() { get { } set { //Await Call to the async func <asyncFunc()> } } I understand we should not make async properties, so what is the optimal way to do this. 回答1: You can't make async properties and you shouldn't want to - properties imply fast , non blocking operations. If you need to perform a long running activity, as implied by you're wanting to kick of an async operation and wait for it, don't make

Make Promise wait for a Chrome.runtime.sendMessage

帅比萌擦擦* 提交于 2021-01-02 05:41:34
问题 I've always seen Promise work with setTimeout, but I'm trying to make it based on whatever the chrome.runtime.sendMessage returns to the Promise. I've got a content script that does this function once the script has been done. chrome.runtime.sendMessage({complete: true}); I've got a background script that loops through every item in an array and uses one of its values to open a URL with chrome.tabs.update. What I'm trying to do is make the async function wait for the Message the content

Make Promise wait for a Chrome.runtime.sendMessage

a 夏天 提交于 2021-01-02 05:38:48
问题 I've always seen Promise work with setTimeout, but I'm trying to make it based on whatever the chrome.runtime.sendMessage returns to the Promise. I've got a content script that does this function once the script has been done. chrome.runtime.sendMessage({complete: true}); I've got a background script that loops through every item in an array and uses one of its values to open a URL with chrome.tabs.update. What I'm trying to do is make the async function wait for the Message the content

Asynchronous calls with React.useMemo

青春壹個敷衍的年華 提交于 2021-01-02 05:30:15
问题 Scenario is relatively simple: we have a long-running, on-demand calculation that occurs on a remote server. We want to memoize the result. Even though we are fetching asychronously from a remote resource, this isn't a side effect because we just want the result of this calculation to display to the user and we definitely don't want to do this on every render. Problem: it seems that React.useMemo does not directly support Typescript's async/await and will return a promise: //returns a promise

Executing run_coroutine_threadsafe in a separate thread

丶灬走出姿态 提交于 2021-01-01 04:20:09
问题 I have a script that is constantly running forever (it checks changes in files). I need to send Discord messages whenever a weird file is made. Problem is, the event watching function ( def run(self): below) is from a subclass, so I can't change it to async def run(self): . Therefore I can't use await channel.send() My solution to this was to use run_coroutine_threadsafe like explained here: https://stackoverflow.com/a/53726266/9283107. That works good! But the problem is, the messages get

Executing run_coroutine_threadsafe in a separate thread

别来无恙 提交于 2021-01-01 04:20:08
问题 I have a script that is constantly running forever (it checks changes in files). I need to send Discord messages whenever a weird file is made. Problem is, the event watching function ( def run(self): below) is from a subclass, so I can't change it to async def run(self): . Therefore I can't use await channel.send() My solution to this was to use run_coroutine_threadsafe like explained here: https://stackoverflow.com/a/53726266/9283107. That works good! But the problem is, the messages get

Why is Task.WhenAll() blocking here?

我只是一个虾纸丫 提交于 2020-12-31 11:57:46
问题 I have a fairly simple code snippet but it seems it blocks on Task.WhenAll(). The Main function just calls new Test() . Can someone help find the root cause? internal class Test { public static async Task<int> Foo() { return 0; } static Test() { var taskList = new List<Task>(); for (int i = 0; i < 100; i++) taskList.Add(Task.Run(() => Foo())); Task.WhenAll(taskList.ToArray()).GetAwaiter().GetResult(); } } 回答1: The CLR takes a global lock when running a static constructor. The static

Why is Task.WhenAll() blocking here?

試著忘記壹切 提交于 2020-12-31 11:52:31
问题 I have a fairly simple code snippet but it seems it blocks on Task.WhenAll(). The Main function just calls new Test() . Can someone help find the root cause? internal class Test { public static async Task<int> Foo() { return 0; } static Test() { var taskList = new List<Task>(); for (int i = 0; i < 100; i++) taskList.Add(Task.Run(() => Foo())); Task.WhenAll(taskList.ToArray()).GetAwaiter().GetResult(); } } 回答1: The CLR takes a global lock when running a static constructor. The static

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

耗尽温柔 提交于 2020-12-31 10:54:55
问题 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