async-await

SendMailAsync : An asynchronous module or handler completed while an asynchronous operation was still pending

ⅰ亾dé卋堺 提交于 2019-12-22 04:09:42
问题 While using SendMailAsync I am getting the following error: An asynchronous module or handler completed while an asynchronous operation was still pending My code : public static async Task SendEmail(MessageContent messageContent, string emailBody) { SmtpClient smtpClientNoSend = new SmtpClient(); await smtpClientNoSend.SendMailAsync(mailMessage); } Call From Controller: public async System.Threading.Tasks.Task<ActionResult> Register() { await SendEmail(); } private void SendEmail() {

How do I await an async method in F#

旧街凉风 提交于 2019-12-22 04:08:13
问题 How do I await an async method in F#? I have the following code: type LegoExample() = let brick = Brick(BluetoothCommunication("COM3")) let! result = brick.ConnectAsync(); Error: Unexpected binder keyword in member definition Note, I reviewed the following link: However, I only observe the functional technique and not the OOP technique. 回答1: you get the error because the let! construct (just like the do! ) needs to be placed inside a computational workflow (like async { ...} but there are

Javascript : promise chain vs. async/await?

落爺英雄遲暮 提交于 2019-12-22 03:56:14
问题 I am learning about Javascript Promise and async / await . The sample code below asynchronously reads and parses a JSON file in node.js ( my node.js version is v10.0.0 ). In the sample code, ChainReadJson function and AwaitReadJson function are doing the same thing, reading and parsing a JSON file. The difference is that ChainReadJson function uses a promise chain, while AwaitReadJson function uses async/await. const FS = require("fs"); function ReadFile(fileName) { return new Promise(

How is async/await working in serial and parallel?

主宰稳场 提交于 2019-12-22 03:55:18
问题 I have two async functions. Both of them are waiting for two 3 seconds function calls. But the second one is faster than the first. I think the faster one is running in parallel and other in serial. Is my assumption correct? If yes, why is this happening as both the functions look logically same? function sleep() { return new Promise(resolve => { setTimeout(resolve, 3000); }); } async function serial() { await sleep(); await sleep(); } async function parallel() { var a = sleep(); var b =

Is it legitimate to omit the 'await' in some cases?

你说的曾经没有我的故事 提交于 2019-12-22 03:51:01
问题 I am using async / await in several places in my code. For example, if I have this function: async function func(x) { ... return y; } Then I always call it as follows: async function func2(x) { let y = await func(x); ... } I have noticed that in some cases, I can omit the await and the program will still run correctly, so I cannot quite figure out when I must use await and when I can drop it. I have concluded that it is "legitimate" to drop the await only directly within a return statement.

IDisposable.Dispose() not called in Release mode for async method

醉酒当歌 提交于 2019-12-22 03:25:20
问题 I wrote the following WPF sample app in VB.NET 14 using .NET 4.6.1 on VS2015.1: Class MainWindow Public Sub New() InitializeComponent() End Sub Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs) MessageBox.Show("Pre") Using window = New DisposableWindow() window.Show() For index = 1 To 1 Await Task.Delay(100) Next End Using MessageBox.Show("Post") End Sub Class DisposableWindow Inherits Window Implements IDisposable Public Sub Dispose() Implements IDisposable.Dispose Me

Asynchronous MVVM commands

六月ゝ 毕业季﹏ 提交于 2019-12-22 01:34:29
问题 I have been following the rather excellent series of articles by Stephen Cleary in the MSDN magazine (Patterns for Asynchronous MVVM Applications) and have been using his IAsyncCommand pattern in a "hello world" style application. However, one area that he does not address is when one needs to pass in a Command Parameter (using this pattern). For a trivial example, take Authentication where the Password control may not be data-bound for security reasons. I wonder if anyone had managed to get

Can I generate an async method dynamically using System.Linq.Expressions?

南笙酒味 提交于 2019-12-22 01:28:11
问题 I know the compiler can't convert an async lambda expression to an expression tree, but is it possible to generate the expression tree manually ? var expr = Expression.Lambda<Func<Task>>( // how do I use 'await' in the body here? ); var func = expr.Compile(); I can't find any method related to async or await in the Expression class, but perhaps there's another way? 回答1: await involves significant compiler re-writing; the generated IL is quite dissimilar to the original C#, with variable

xUnit and Moq do not support async - await keywords

送分小仙女□ 提交于 2019-12-22 01:27:07
问题 I am trying to discover how to apply the async and await keywords to my xUnit tests. I am using xUnit 1.9 and Async CTP 1.3. Here is my test case I have an interface which specifies one asynchronous method call public interface IDoStuffAsync { Task AnAsyncMethod(string value); } I have a class which consumes the interface and calls the async method public class UseAnAsyncThing { private readonly IDoStuffAsync _doStuffAsync; public UseAnAsyncThing(IDoStuffAsync doStuffAsync) { _doStuffAsync =

ConfigureAwait when not awaiting

怎甘沉沦 提交于 2019-12-22 00:02:22
问题 I have an async method I am using to offload a few seconds' worth of fire-and-forget work so as not to slow down my page load. This work needs a bit of general setup and tidy-up; I want the (fast) setup to throw synchronously if it throws, but I don't want to force the tidy-up to run in the ASP context so I am using ConfigureAwait on the bit I am awaiting: public Task FireAndForget() { DoSetup(); return FireAndForgetAfterSetup(); } private async Task FireAndForgetAfterSetup() { await