Is it possible to await an event instead of another async method?

In my C#/XAML metro app, there’s a button which kicks off a long-running process. So, as recommended, I’m using async/await to make sure the UI thread doesn’t get blocked: private async void Button_Click_1(object sender, RoutedEventArgs e) { await GetResults(); } private async Task GetResults() { // Do lot of complex stuff that takes a long … Read more

Suppressing “warning CS4014: Because this call is not awaited, execution of the current method continues…”

This is not a duplicate of “How to safely call an async method in C# without await”. How do I nicely suppress the following warning? warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the ‘await’ operator to the result of the call. … Read more

Using filesystem in node.js with async / await

I would like to use async/await with some filesystem operations. Normally async/await works fine because I use babel-plugin-syntax-async-functions. But with this code I run into the if case where names is undefined: import fs from ‘fs’; async function myF() { let names; try { names = await fs.readdir(‘path/to/dir’); } catch (e) { console.log(‘e’, e); } … Read more

try/catch blocks with async/await

I’m digging into the node 7 async/await feature and keep stumbling across code like this function getQuote() { let quote = “Lorem ipsum dolor sit amet, consectetur adipiscing elit laborum.”; return quote; } async function main() { try { var quote = await getQuote(); console.log(quote); } catch (error) { console.error(error); } } main(); This seems … Read more

Using ‘async’ in a console application in C# [duplicate]

This question already has answers here: Can’t specify the ‘async’ modifier on the ‘Main’ method of a console app (17 answers) Closed 5 years ago. I have this simple code: public static async Task<int> SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task<int> GetOperationOneAsync() … Read more

Parallel.ForEach vs Task.Run and Task.WhenAll

What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously? Version 1: List<string> strings = new List<string> { “s1”, “s2”, “s3” }; Parallel.ForEach(strings, s => { DoSomething(s); }); Version 2: List<string> strings = new List<string> { “s1”, “s2”, “s3” }; List<Task> Tasks = new List<Task>(); foreach (var s in … Read more

How to find which promises are unhandled in Node.js UnhandledPromiseRejectionWarning?

Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often: (node:11057) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: Error: Can’t set headers after they are sent. (node:11057) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not … Read more

What’s the difference between Task.Start/Wait and Async/Await?

I may be missing something but what is the difference between doing: public void MyMethod() { Task t = Task.Factory.StartNew(DoSomethingThatTakesTime); t.Wait(); UpdateLabelToSayItsComplete(); } public async void MyMethod() { var result = Task.Factory.StartNew(DoSomethingThatTakesTime); await result; UpdateLabelToSayItsComplete(); } private void DoSomethingThatTakesTime() { Thread.Sleep(10000); } 6 Answers 6