async-await

Cancellation Token in await method

情到浓时终转凉″ 提交于 2019-12-19 07:45:12
问题 There are many reasons to put a token in the constructor of a task, mentioned here: Cancellation token in Task constructor: why? With the use of keywords, async / await, how is that working? for example my code below: public async Task MethodAsync(CancellationToken token) { await Method01Async(); await Method02Async(); } Although it is an asynchronous process. In no time I used "Task.StartNext" or "Task.Run" or "new Task". To be able to specify my cancellation token, how can I do? 回答1: You

Why Does My Asynchronous Code Run Synchronously When Debugging?

允我心安 提交于 2019-12-19 05:32:25
问题 I am trying to implement a method called ReadAllLinesAsync using the async feature. I have produced the following code: private static async Task<IEnumerable<string>> FileReadAllLinesAsync(string path) { using (var reader = new StreamReader(path)) { while ((await reader.ReadLineAsync()) != null) { } } return null; } private static void Main() { Button buttonLoad = new Button { Text = "Load File" }; buttonLoad.Click += async delegate { await FileReadAllLinesAsync("test.txt"); //100mb file!

How can I unit test this async method which (correctly) throws an exception?

旧巷老猫 提交于 2019-12-19 05:22:33
问题 I have the following method in an interface.. Task<SearchResult<T>> SearchAsync(TU searchOptions); works great. Now i'm trying to make a unit test to test when something goes wrong - and the code throws an exception. In this case, I've setup my method to throw an HttpRequestException . My unit test fails to say that I threw that exception .. var result = Should.Throw<HttpRequestException> (async () => await service.SearchAsync(searchOptions)); the error message from the unit test is Shouldly

Call multiple async methods that rely on each other

我怕爱的太早我们不能终老 提交于 2019-12-19 04:55:34
问题 I'm looking for best practice around calling multiple async methods where each next method relies on the values returned from one before. I'm experimenting with 2 approaches 1) https://dotnetfiddle.net/waPL9L public async void Main() { var T1 = await Sum(2,5); var T2 = await Sum(T1, 7); var T3 = await Sum(T2, 7); Console.WriteLine(T3); } public async Task<int> Sum(int num1, int num2){ return await Task.Run(() => { // for some reason if i use Sleep... I don't see any results at all... //Thread

Async and asynchronous methods clarification?

女生的网名这么多〃 提交于 2019-12-19 04:23:49
问题 AFAIK - ( and I read a lot about it), asynchronous methods ( not asynchronous delegates !) exists to solve the "thread is blocked" problem when dealing with I/O operations like : reading a file or downloading a file : Richter shows it quite clearly here : Task<T> is not related to the i/o blocking issue. it is simply just like open a thread ( plus extra efficiency + functionality ) - but it still causes a thread to consume cpu quanta etc. And here is my question : I've read (msdn) that : An

StreamWriter: (Write+Flush)Async seem much slower then synchronous variants

烈酒焚心 提交于 2019-12-19 04:16:07
问题 I was trying to find an IO bottleneck in my class and surprisingly noticed that sw.Write("m"); sw.Flush() can be 20000 faster then await sw.WriteAsync("m"); Await sw.FlushAsync(); when writing 1000000 messages to a file. Does, by any chance, anyone know why? My bet is StreamWriter 's constructor taking a String does not parametrize a stream for async usage. The code below can be launched from C# interactive. Yes, it's not the best place to measure the speed but it will show the matter at hand

How to wait until all tasks are finished before running code

和自甴很熟 提交于 2019-12-19 03:54:07
问题 I am trying to write a multi threading search and then display all the results once the tasks have finished running but currently I don't understand how to process the results once all the tasks are complete My code is as follows: private async void DoSearchAsync() { var productResults = await SearchProductsAsync(CoreCache.AllProducts); var brochureResults = await SearchBrochuresAsync(CoreCache.AllBrochures); _searchResults.AddRange(productResults); _searchResults.AddRange(brochureResults);

How can I use ES2016 (ES7) async/await in my acceptance tests for a Koa.js app?

半世苍凉 提交于 2019-12-19 03:47:05
问题 I am in the process of writing my first Koa.js app, and having recently been introduced to the ES2016 (aka ES7) features of async / await , I wanted to make use of these. I found that my Google skills were not up to the task, and the few snippets of code I could find were either for the standard Koa (using generators) or otherwise not-as-bleeding-edge-as-ES7. See my answer below for how I got my tests running. 回答1: I'm still a beginner, so it's likely that a lot of this can be optimised

Synchronous or asynchronous continuation upon TaskCompletionSource.TrySetResult?

自作多情 提交于 2019-12-19 03:39:16
问题 How to tell if the continuation initiated by TaskCompletionSource.TrySetResult is going to be executed synchronously or asynchronously? For example: // class A void RegisterNotification(TaskCompletionSource<object> tcs) { this.source.NotificationEvent += (s, eData) => { Debug.WriteLine("A.before"); tcs.TrySetResult(eData.Result); Debug.WriteLine("A.after"); DoProcessingA(); }; } // class B async Task RequestNotificationAsync() { var tcs = new TaskCompletionSource<object>(); this.a

Sending async mail from SignalR hub

限于喜欢 提交于 2019-12-19 03:24:26
问题 I need to send an email as a result of a SignalR hub invocation. I don't want the send to execute synchronously, as I don't want to tie up WebSocket connections, but I would like the caller to be informed, if possible, if there were any errors. I thought I'd be able to use something like this in the hub (minus error handling and all other things that I want it to do): public class MyHub : Hub { public async Task DoSomething() { var client = new SmtpClient(); var message = new MailMessage(/*