async-await

c# webapi: Await Task.Run vs more granualar await

北城余情 提交于 2019-12-23 04:37:21
问题 I'm using async/await in WebApi Controllers according to this article: https://msdn.microsoft.com/en-us/magazine/dn802603.aspx Hava look at this simplified code in my controller: DataBaseData = await Task.Run( () => GetDataFunction() ); GetDataFunction is a function that will open a database connection, open a reader and read the data from the database. In many examples I see it handled differently. The GetDataFunction itself is awaited. And within the function every single step is awaited.

Async methods with or without ConfigureAwait on NetStandard lib for Net Core

耗尽温柔 提交于 2019-12-23 04:36:35
问题 I have a library that is doing bulk insert. Library is EfCore extension made in .NetStandard(1.4) so it can be used in ASP.NET Core projects targeting both .NetCore(1.0+) or full NetFramework(4.6.1+) One of the functions is: public static BulkInsert<T>(this DbContext context, IList<T> entities) { SqlBulkOperation.InsertAsync<T>(context, entities); } internal static class SqlBulkOperation { public static void Insert<T>(DbContext context, IList<T> entities) { .... sqlBulkCopy.WriteToServer

How can I use async/await while assigning a variable?

半世苍凉 提交于 2019-12-23 04:34:13
问题 I have a variable I'm setting based on the result of an async function, the variable is initialState const initialState = await getContacts() .then((body) => { console.log('body: '); console.log(body); return body; }).catch(err => console.log(err)); My getContacts() returns a promise that should be the result of this function: export async function getContacts() { let data = fetch('/api/contacts').then((data) => { console.log(data); return data.json(); }).catch(err => console.log(err));

Sync to async dispatch: how can I avoid deadlock?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 04:28:45
问题 I'm trying to create a class that has synchronous methods and calls some other library methods which are asynchronous. For that reason I use Task.Result to wait for the async operation to finish. My methods are called by WPF app in synchronous way. This leads to a deadlock. I know that the best way is to make all my methods asynchronous but my situation requires them to be synchronous. From the other hand they use other library which is asynchronous. My question is: How can I avoid the

How can I control thread count when I use “Task.WhenAll”

感情迁移 提交于 2019-12-23 02:59:04
问题 I am verifying image urls by making an http get request asynchronously. All works fine with the code below but when I have so many Images, our firewall will block my internet access because of so many threads concurrently requesting. Therefore I was looking for a solution how to restrict the count of concurrently running threads. I ended up with this thread telling me to use SemaphoreSlim but I am somehow not able to get the idea and how to implement this? is that SemaphoreSlim wait or

Error POST http:abc/ 404 (Not Found) in angular2+ and nodejs

99封情书 提交于 2019-12-23 02:44:10
问题 I am making a http request from my angular2+ code to database present in node.js file. The ajax call from angular2+ hits the controller.js file and then redirects to service.js file which has the connection to the database: angular.ts ==> controller.js ==> service.js From the database the service.js file gives the output to controller.js file and then answers the ajax call to angular.ts file: service.js ==> controller.js ==> angular.ts However, I am getting the error: POST http://localhost

C# running async tasks in sync context

主宰稳场 提交于 2019-12-23 02:39:22
问题 I don't have much experience in C# async. Task - load bitmaps from the internet. Before, I was just loading them 1 by 1, in sync. Loading them in async would give results quicker. Down below, I made two examples, how I could get single image - GetImage and GetImageAsync . And for a list of Images, I would use LoadImages and LoadImages2 . LoadImages would run sync functions in async (all at the same time (?)), LoadImages2 would run async functions in async and produce the same result (?). The

async await execution windows phone 8

半城伤御伤魂 提交于 2019-12-23 02:28:35
问题 I am new to async and await style of programming. How can I solve the following problem: I am calling the below code first. The problem here is that the first line is awaiting which need to populate the categoriesvm.Categorieslist , which it does not, but the second line is called. (which I think is the default behaviour of await) How can I make sure that the second line is called only when the categoriesvm.Categorieslist is populated in the first line? protected override void OnNavigatedTo

How to cancel Stream.ReadAsync?

守給你的承諾、 提交于 2019-12-23 01:52:58
问题 Why doesn't the code below ever complete if you don't type any input, and why does it still respond to a key being pressed even after the cancellation token has been canceled? // Set up a cancellation token var cancellationSource = new CancellationTokenSource(); // Cancel the cancellation token after a little bit of time Task.Run(async () => { await Task.Delay(TimeSpan.FromSeconds(2)); cancellationSource.Cancel(); Console.WriteLine("Canceled the cancellation token"); }); // Wait for user

Async/Await equivalent to .ContinueWith with CancellationToken and TaskScheduler.FromCurrentSynchronizationContext() scheduler

谁说我不能喝 提交于 2019-12-23 01:52:55
问题 This is a follow-up to this question. Question : What would be a succinct way to express the following using async / await instead of .ContinueWith() ?: var task = Task.Run(() => LongRunningAndMightThrow()); m_cts = new CancellationTokenSource(); CancellationToken ct = m_cts.Token; var uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task updateUITask = task.ContinueWith(t => UpdateUI(t), ct, TaskContinuationOptions.None, uiTaskScheduler); I'm mainly interested in the case