async-await

async/await function comparison

我们两清 提交于 2020-01-30 02:41:16
问题 I am trying to understand async/await and I am wondering if both methods work identically.If not can you explain why? public async Task<Client> GetClient() { return await _clientRepository.GetAll().Where(x => x.Id == 1).FirstOrDefaultAsync(); } public Task<Client> GetClient2() { return Task.FromResult(_clientRepository.GetAll().Where(x => x.Id == 1).FirstOrDefault()); } public async Task Run() { var result = await GetClient(); var result2 = await GetClient2(); } 回答1: Let me point out a few

Capturing Exceptions on async operations

梦想与她 提交于 2020-01-29 11:50:10
问题 I'm reading up more about async here: http://msdn.microsoft.com/en-us/library/hh873173(v=vs.110).aspx Going through this example: Task<bool> [] recommendations = …; while(recommendations.Count > 0) { Task<bool> recommendation = await Task.WhenAny(recommendations); try { if (await recommendation) BuyStock(symbol); break; } catch(WebException exc) { recommendations.Remove(recommendation); } } I wonder, if I'm already performing await on Task.WhenAny why do I need to await again inside of the

async Task<IEnumerable> with yield return?

放肆的年华 提交于 2020-01-29 03:28:30
问题 The below method doesn't compile. Alternatives? public static async Task<IEnumerable<object[]>> GetRecordsAsync( this Transaction transaction, string commandText, params SqlParameter[] parameters) { // Get a SqlDataReader var reader = await transaction.GetReaderAsync(commandText, parameters); var fieldCount = -1; // Begin iterating through records asynchronously while (await reader.ReadAsync()) // Note we don't loop until .ReadAsync returns a boolean { // Grab all the field values out if

Why is calling await completing the parent Task prematurely?

依然范特西╮ 提交于 2020-01-28 11:20:13
问题 I'm trying to create a control that exposes a DoLoading event that consumers can subscribe to in order to perform loading operations. For convenience, event handlers should be called from the UI thread allowing consumers to update the UI at will, but they will also be able to use async/await to perform long-running tasks without blocking the UI thread. For this, I have declared the following delegate: public delegate Task AsyncEventHandler<TEventArgs>(object sender, TEventArgs e); That allows

Use an async callback with Task.ContinueWith

本秂侑毒 提交于 2020-01-27 03:50:06
问题 I'm trying to play a bit with C#'s async/await/continuewith. My goal is to have to have 2 tasks which are running in parallel, though which task is executing a sequence of action in order. To do that, I planned to have a List<Task> that represent the 2 (or more) tasks running in parallel, and to use ContinueWith on each of the Task My problem is that the callback in continue with seems not to be executed while the await taskList has already returned. In order to summarize, here's a sample to

Returning an object from a callback using async/await

北城余情 提交于 2020-01-26 03:54:07
问题 I can't resolve this question with the answers to this question because there are differences in the code. I want to return an object out of a callback. When I run the below code, the log of the body object looks as expected. It appears to be the correct JSON object containing the response I want from the server: name, email, website, etc. But the result object appears to contain information about the request itself instead of the response object. How do I return the body object so that I can

Error calling async method synchronously

半腔热情 提交于 2020-01-25 12:09:22
问题 I'm writing code in a desktop application that will be used in police cars to use the new Windows Geolocation API. I've written an event listener for the Geolocator.PositionChanged event, but it's not getting called. In my reading of the documentation, it seems that the Gelocator only raises this event when the position changes. I figure the first thing my program should do after it finished setting up the event listener is to call the Geolocator.GetPositionAsync method to get the current

async-await not working when exporting Google distance Matrix from a class

元气小坏坏 提交于 2020-01-25 09:18:06
问题 I'm using the google places autocomplete and the Distance Matrix to put two locations in and print out the distance between the two. I have two javascript es6 classes to separate the Google API's as services. My problem is I'm trying to await the response on getting the distance from the distance matrix service then log it to the console, but I get undefined when I log it. the distance matrix service is returning the response after I log it. This must mean that my await in the method is not

Replace Task.WhenAll with PLinq

混江龙づ霸主 提交于 2020-01-25 06:39:08
问题 I'm having a method which calls a WCF service multiple times in parallel. To prevent an overload on the target system, I want to use PLinq's ability to limit the number of parallel executions. Now I wonder how I could rewrite my method in an efficient way. Here's my current implementation: private async Task RunFullImport(IProgress<float> progress) { var dataEntryCache = new ConcurrentHashSet<int>(); using var client = new ESBClient(); // WCF // Progress counters helpers float totalSteps = 1f

SyntaxError: await is only valid in async function, When connecting to Mongo DB using Node JS

坚强是说给别人听的谎言 提交于 2020-01-25 05:09:06
问题 I am trying to access a mongo database using an async / await function in Javascript using the code provided below. When I run the code, the terminal returns the following error: SyntaxError: await is only valid in async function The error is confusing to me, because of my use of "async" for newFunction. I have tried changing the location of "async" and "await," but no combination that I have tried so far has yielded successful execution. Any insight would be very much appreciated. var