async-await

Trying to call Async method synchronously. It waits on Task.Result forever [duplicate]

喜夏-厌秋 提交于 2019-12-24 03:12:51
问题 This question already has answers here : Synchronously waiting for an async operation, and why does Wait() freeze the program here (4 answers) Closed 5 years ago . So I'm writing an application in which I want to expose a series of methods with both synchronous and asynchronous equivalents. To do this, I figured the easiest approach was to write the logic in the asnyc method, and write synchronous methods as wrappers around the async methods, waiting synchronously for them to deliver their

Trying to call Async method synchronously. It waits on Task.Result forever [duplicate]

泄露秘密 提交于 2019-12-24 03:12:03
问题 This question already has answers here : Synchronously waiting for an async operation, and why does Wait() freeze the program here (4 answers) Closed 5 years ago . So I'm writing an application in which I want to expose a series of methods with both synchronous and asynchronous equivalents. To do this, I figured the easiest approach was to write the logic in the asnyc method, and write synchronous methods as wrappers around the async methods, waiting synchronously for them to deliver their

Does a loop with Task.Delay() create a memory leak?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 03:07:49
问题 I am implementing an asynchronous buffer system where I want exactly one consumer of a queue to guarantee that items are processes in order. The consumer should check the queue periodically, process all items in it, and then "Sleep" for some interval. Task.Delay() seems perfect for such a system, since unlike Thread.Sleep() it won't consume a thread while sleeping and unlike Timer it won't launch a new thread if processing the queue items takes longer than the sleep interval. However, I'm

How to run a background process in response to selection change event in VSTO Office add-in?

给你一囗甜甜゛ 提交于 2019-12-24 02:51:11
问题 I have an VSTO application-level add-in, with my own custom task pane. I'm trying to intercept the SelectionChange event, and display information in my custom task pane that's relevant to the selection. I do know how to do that synchronously. However the process of fetching the information to display is slow, and I don't want to block the application while I fetch the information. Furthermore, the user may change the selection several times, and I want to cancel any in-progress "fetch"

Why even after using async await for multiple calls still got empty response?

ε祈祈猫儿з 提交于 2019-12-24 01:15:36
问题 Here is simple node route in which calling an asynchronous api . What needed is to return data after the looping. But It is returning blank object. try { const array = ["brunch", "lunch", "crunch"] const data = {} array.map(async(d) => { const venue = await Venue.find({ "category": { "$in": [d] }}) data[d] = venue }) return data } catch(err) { throw err } Please help me to achieve this 回答1: There is a better way to get the desired result with MongoDB and no need to loop, use the aggregation

Mongoose await save

删除回忆录丶 提交于 2019-12-24 01:01:45
问题 In my Nodejs project I would just like to await a mongoose.save function before continuing with my code. The below example does not work, but can anyone help me with something that will work please. app.post('/api/CreateUser', async (req, res) => { const newUser = new User({ 'email': req.body.email, 'name': req.body.name }); console.log('before save'); await newUser.save((err, userDoc) => { if (err) return res.status(400).send(err); console.log('saved item'); }); console.log('after save'); })

Manually capturing and applying SynchronizationContext when completing a Task

早过忘川 提交于 2019-12-24 00:43:56
问题 I was having a problem with a hanging await (described here). During research I found out that calling SetResult on my TaskCompletionSource actually invokes awaiting continuation in the context of the thread that called SetResult (this is also spelled out in this answer to a somewhat related question). In my case this is a different thread (a thread-pool worker thread) from the one that started the await (an ASP.NET request thread). While I'm still not sure why this would cause a hang, I

How to use Array.prototype.some() with an async function?

扶醉桌前 提交于 2019-12-24 00:42:17
问题 I am trying to do the following: command.permissions.some(async permissionsKey => { switch (permissionsKey) { case "all": { return true; } case "OWNER": { return await msg.roomContext.isRoomOwnerId(msg.getStaticUserUID()); } default: { return config.users_groups[permissionsKey].includes(msg.getStaticUserUID()); } } }); However it always is true because Array.prototype.some does not expect an async function so a promise is returned when the function is called. A Promise is truthy. I was

how to cancel HostingEnvironment.QueueBackgroundWorkItem

强颜欢笑 提交于 2019-12-24 00:38:28
问题 Is there a way to cancel background task made with HostingEnvironment.QueueBackgroundWorkItem ? There is CancellationToken which notifies if tasks was cancelled but how can i do it? Refering to https://msdn.microsoft.com/en-us/library/dd997396(v=vs.110).aspx A successful cancellation involves the requesting code calling the CancellationTokenSource.Cancel method OK. Where can i get access to CancellationTokenSource ? 回答1: After few trials i came up with the soulution: HostingEnvironment

Return value from an async function

﹥>﹥吖頭↗ 提交于 2019-12-24 00:09:20
问题 How can I return the value from an async function? I've the following Promise: function reqGitActivity (url) { const options = { url: url, headers: { 'User-Agent': 'request' } } return new Promise((resolve, reject) => { request(options, (err, res, body) => { if (err) { reject(err) return } resolve(body) }) }) } Then I use this Promise with Async/Await async function githubActivity () { const gh = await reqGitActivity(`https://api.github.com/users/${github}/events`) return gh } And if I