async-await

How do I convert this to an async task?

微笑、不失礼 提交于 2019-12-28 20:41:35
问题 Given the following code... static void DoSomething(int id) { Thread.Sleep(50); Console.WriteLine(@"DidSomething({0})", id); } I know I can convert this to an async task as follows... static async Task DoSomethingAsync(int id) { await Task.Delay(50); Console.WriteLine(@"DidSomethingAsync({0})", id); } And that by doing so if I am calling multiple times (Task.WhenAll) everything will be faster and more efficient than perhaps using Parallel.Foreach or even calling from within a loop. But for a

Cancel NetworkStream.ReadAsync using TcpListener

℡╲_俬逩灬. 提交于 2019-12-28 18:39:03
问题 Consider the following simplified example (ready to roll in LinqPad, elevated account required): void Main() { Go(); Thread.Sleep(100000); } async void Go() { TcpListener listener = new TcpListener(IPAddress.Any, 6666); try { cts.Token.Register(() => Console.WriteLine("Token was canceled")); listener.Start(); using(TcpClient client = await listener.AcceptTcpClientAsync() .ConfigureAwait(false)) using(var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5))) { var stream=client.GetStream

Preventing a deadlock when calling an async method without using await

久未见 提交于 2019-12-28 16:51:53
问题 I need to call a method returning a Task from within public override void OnActionExecuting(ActionExecutingContext filterContext) It wont let me make this method async it throws the following An asynchronous module or handler completed while an asynchronous operation was still pending. and when calling entityStorage.GetCurrentUser().Result I get a deadlock. How can I avoid this? I have been playing around with it coming up with stuff like entityStorage.GetCurrentUser().Result.ConfigureAwait

Why does WebClient.DownloadStringTaskAsync() block ? - new async API/syntax/CTP

假装没事ソ 提交于 2019-12-28 13:44:15
问题 For some reason there is a pause after the program below starts. I believe that WebClient().DownloadStringTaskAsync() is the cause. class Program { static void Main(string[] args) { AsyncReturnTask(); for (int i = 0; i < 15; i++) { Console.WriteLine(i); Thread.Sleep(100); } } public static async void AsyncReturnTask() { var result = await DownloadAndReturnTaskStringAsync(); Console.WriteLine(result); } private static async Task<string> DownloadAndReturnTaskStringAsync() { return await new

Unhandled exception handler not called for Metro / WinRT UI async void event handler

巧了我就是萌 提交于 2019-12-28 13:22:48
问题 Consider the following to be extracts from a Windows 8 Metro / WinRT app, which have been reduced to the bare minimum required to show the anomaly: public class App : Application { public App() { UnhandledException += (sender, e) => e.Handled = true; } } public class MainPage : Page { private void Button_Click_1(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } private async void Button_Click_2(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } }

Unhandled exception handler not called for Metro / WinRT UI async void event handler

主宰稳场 提交于 2019-12-28 13:22:15
问题 Consider the following to be extracts from a Windows 8 Metro / WinRT app, which have been reduced to the bare minimum required to show the anomaly: public class App : Application { public App() { UnhandledException += (sender, e) => e.Handled = true; } } public class MainPage : Page { private void Button_Click_1(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } private async void Button_Click_2(object sender, RoutedEventArgs e) { throw new NotSupportedException(); } }

ES2017 - Async vs. Yield

我的梦境 提交于 2019-12-28 12:13:31
问题 I am confused about the current discussion of adding async functions and the keyword await to the next EcmaScript. I do not understand why it is necessary to have the async keyword before the function keyword. From my point of view the await keyword to wait for a result of a generator or promise done , a function's return should be enough. await should simple be usable within normal functions and generator functions with no additional async marker. And if I need to create a function what

Converting a WebClient method to async / await

百般思念 提交于 2019-12-28 11:43:01
问题 I have some existing code which I am porting to Windows 8 WinRT. The code fetches data from URL, asynchronously invoking a passed delegate: private void RequestData(string uri, Action<string> action) { var client = new WebClient(); client.DownloadStringCompleted += (s,e) => action(e.Result); client.DownloadStringAsync(new Uri(uri)); } Converting to WinRT requires the use of HttpClient and asynchronous methods. I've read a few tutorials on async / await, but am a bit baffled. How can I change

How to use RestSharp with async/await

落爺英雄遲暮 提交于 2019-12-28 09:31:13
问题 I'm struggling to find a modern example of some asynchronous C# code that uses RestSharp with async and await . I know there's been a recent update by Haack but I don't know how to use the new methods. Also, how can I provide a cancellation token so that the operation can be canceled (say, if a person is sick of waiting and presses the Cancel button in the app's UI). 回答1: Well, the update Haack is referring to has been made by me :) So let me show you how to use it, as it is actually very

Get Bluebird Promise from async await functions

狂风中的少年 提交于 2019-12-28 06:01:14
问题 I am looking for a way, with Node v7.6 or above, to get a Bluebird Promise (or any non-native promise) when an async function is called. In the same way I can do: global.Promise = require('Bluebird'); // Or Q/When var getResolvedPromise = () => Promise.resolve('value'); getResolvedPromise .tap(...) // Bluebird method .then(...); See: May I use global.Promise=require("bluebird") I want to be able to do something like: global.Promise = require('Bluebird'); // Or Q/When var