async-await

Why does Task.WaitAll() not block or cause a deadlock here?

我只是一个虾纸丫 提交于 2019-12-18 02:42:41
问题 In the example below two await calls are used. To gain performance, the sample gets converted Task.WaitAll() instead (not really any faster, but this is just an example). This is code from a library using Sqlite.Net on Android and the method gets called from OnResume() on the main UI thread: public async Task SetupDatabaseAsync() { await CreateTableAsync<Session>(); await CreateTableAsync<Speaker>(); } Here's the alternative: public void SetupDatabaseAsync() { var t1 = CreateTableAsync

What is the benefit to using await with an async database call

南笙酒味 提交于 2019-12-18 02:40:09
问题 I am just looking at the default MVC5 project and how it uses async in the controllers. I would like to know what benefit async provides here over simply using synchronous calls: [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Disassociate(string loginProvider, string providerKey) { ManageMessageId? message = null; //why use an async database call here with await instead of just using a synchronous one? IdentityResult result = await UserManager.RemoveLoginAsync(User

How to cancel await Task.Delay()?

若如初见. 提交于 2019-12-18 02:02:32
问题 As you can see in this code: public async void TaskDelayTest() { while (LoopCheck) { for (int i = 0; i < 100; i++) { textBox1.Text = i.ToString(); await Task.Delay(1000); } } } I want it to set textbox to string value of i with one second period until I set LoopCheck value to false . But what it does is that it creates all iteration ones in for all and even if I set LoopCheck value to false it still does what it does asyncronously. I want to cancel all awaited Task.Delay() iteration when I

In C#7, how can I “roll my own” Task-like type to use with async?

瘦欲@ 提交于 2019-12-17 23:37:12
问题 One of the less-talked-about features of C#7 is "generalized async return types", which is described by Microsoft as: Returning a Task object from async methods can introduce performance bottlenecks in certain paths. Task is a reference type, so using it means allocating an object. In cases where a method declared with the async modifier returns a cached result, or completes synchronously, the extra allocations can become a significant time cost in performance critical sections of code. It

Why are await and async valid variable names?

社会主义新天地 提交于 2019-12-17 23:29:48
问题 I was experimenting with how / is interpreted when around different keywords and operators, and found that the following syntax is perfectly legal: // awaiting something that isn't a Promise is fine, it's just strange to do: const foo = await /barbaz/ myFn() Error: Uncaught ReferenceError: await is not defined It looks like it tries to parse the await as a variable name ..? I was expecting await is only valid in async function or maybe something like Unexpected token await To my horror, you

Calling async methods from a Windows Service

早过忘川 提交于 2019-12-17 22:12:27
问题 I have a Windows Service written in C# that periodically fires off background jobs. Typically, at any given time, several dozen heavily I/O bound Tasks (downloading large files, etc) are running in parallel. The service runs on a relatively busy web server (necessary for now), and I think it could benefit greatly in terms of thread conservation to use asynchronous APIs as much as possible. Most of this work is done. All jobs are now fully async (leveraging HttpClient, etc.), as is the main

Poll for a result n times (with delays between attempts) before failing

大城市里の小女人 提交于 2019-12-17 21:00:13
问题 We need to write a Node.js function that polls a certain API endpoint for a result of a previously requested calculation. The result takes a random time to be generated and may not me generated at all. We'd like to get it as soon as possible, but also I don't want to wait for too long, which means that after a certain number of API calls we'd like the function to fail (reject a Promise). There is one way communication between our code and the API. const Bluebird = require('bluebird');

Is it correct if i am using await + ToListAsync() over IQueryable which is not define as a task

血红的双手。 提交于 2019-12-17 20:18:31
问题 I am using asp.net MVC-5 with EF-6, and I am not sure if using await + ToListAsync is valid. For example, I have the following repository method which returns an IQueryable :- public IQueryable<TSet> getAllScanEmailTo() { return t.TSets.Where(a=>a.Name.StartsWith("ScanEmail")); } And I am calling it as follow:- var emailsTo = await repository.getAllScanEmailTo().ToListAsync(); In the beginning, I thought I will get an error because I am using "await" a method which is not defined as a task,

Task from cancellation token?

妖精的绣舞 提交于 2019-12-17 19:57:54
问题 Given a cancellation token, I'd like to create an awaitable task out of it, which is never complete but can be cancelled. I need it for a pattern like this, which IMO should be quite common: async Task DoStuff(Task t, CancellationToken ct) { // t was made from TaskCompletionSource, // both t and ct are beyond my control Task t2 = TaskFromCancellationToken(ct); await Task.WhenAny(t, t2); // do stuff } The best idea I've got so far is this: Task TaskFromCancelationToken(CancellationToken ct) {

Call async method on UI thread

对着背影说爱祢 提交于 2019-12-17 19:52:45
问题 I'm trying to create WPF client with IdentityServer authentication. I'm using their OidcClient to get logged in. It's whole async while my app is sync and can't be refactored without huge effort. Calling var result = await _oidcClient.LoginAsync(); doesn't wait for the result. Calling Wait() or .Result causes deadlock. Wrapping it to other Task.Run is complaining that the method is not running on UI thread (it opens browser with login dialog). Do you have any idea, how to solve this? Do I