async-await

Mixing async/await with Result

微笑、不失礼 提交于 2020-01-01 04:43:05
问题 Let me just preface this question with a few things: I've read several SO questions saying that you should not do this (such as How to safely mix sync and async code) I've read Async/Await - Best Practices in Asynchronous Programming again saying you shouldn't do this So I do know that this is not a best practice, and don't need anyone telling me such. This is more of a "why does this work" question. With that out of the way, here is my question: I've written a small GUI application that has

In which case does TaskCompletionSource.SetResult() run the continuation synchronously?

余生颓废 提交于 2020-01-01 04:32:07
问题 Initially I thought that all continuations are executed on the threadpool (given a default synchronization context). This however doesn't seem to be the case when I use a TaskCompletionSource . My code looks something like this: Task<int> Foo() { _tcs = new TaskCompletionSource<int>(); return _tcs.Task; } async void Bar() { Console.WriteLine(Thread.Current.ManagedThreadId); Console.WriteLine($"{Thread.Current.ManagedThreadId} - {await Foo()}"); } Bar gets called on a specific thread and the

Is my understanding of async/await, how it works and its benefits, correct?

别等时光非礼了梦想. 提交于 2020-01-01 04:04:06
问题 I've asserted my understanding of async/await on a couple of occasions, often with some debate as to whether or not I'm correct. I'd really appreciate it if anyone could either confirm or deny my understanding, and clear up any misconceptions so that I don't spread misinformation. High-Level Understanding async / await is a way of avoiding callback hell while writing asynchronous code. A thread that is executing an asynchronous method will return to the thread pool when it encounters an await

async/await. Where is continuation of awaitable part of method performed?

北城以北 提交于 2020-01-01 03:25:07
问题 I am really curious how async/await enables your program not to be halted. I really like the way how Stephen Cleary explains async/await: "I like to think of "await" as an "asynchronous wait". That is to say, the async method pauses until the awaitable is complete(so it waits), but the actual thread is not blocked (so it's asynchornous)." I've read that async method works synchronously till compilator meets await keywords. Well. If compilator cannot figure out awaitable, then compilator

C# Task thread pool - Running 100 tasks across only 10 threads [duplicate]

独自空忆成欢 提交于 2020-01-01 03:19:05
问题 This question already has answers here : Have a set of Tasks with only X running at a time (5 answers) Closed 3 years ago . I'm just wondering if someone can point me in the right direction about the async/await framework and thread pools? Basically, what I'm trying to do is have x number of operations executed in a separate thread/async, but across a maximum of y threads. For example, let's say that I have 100 database operations: await _repository.WriteData(someData); What I'd like to do is

Does async/await blocks event loop? [duplicate]

丶灬走出姿态 提交于 2020-01-01 03:13:08
问题 This question already has answers here : Will async/await block a thread node.js (5 answers) In JavaScript, does using await inside a loop block the loop? (7 answers) Closed last year . I was reading Don't Block the Event Loop from the Node.js guide. There was a line saying: You should make sure you never block the Event Loop. In other words, each of your JavaScript callbacks should complete quickly. This of course also applies to your await 's, your Promise.then 's, and so on. I started to

How to await an async call in JavaScript in a synchronous function?

人走茶凉 提交于 2020-01-01 02:35:12
问题 I recently had to correct security issues in a web-application (that I didn't create). The security problem was, it was using non-http-only cookies. So I had to set the session-cookie http-only, which means you can't read (and set) the cookie's value anymore from javascript. So far so seamingly easy. The deeper problem was, the web-application used JSON.parse(readCookie(cookieName)).some_value on a million places . So in order to not have to re-write "a million lines of code", I had to create

best practice for using async await in webapi

北城余情 提交于 2020-01-01 02:34:14
问题 I have .NET core Web API which as service layer. Service layer has all EF code. If have basecontroller with this code protected Task<IActionResult> NewTask(Func<IActionResult> callback) { return Task.Factory.StartNew(() => { try { return callback(); } catch (Exception ex) { Logger.LogError(ex.ToString()); throw; } }); } In controller action I wrap all calls to service in above method e.g. : [HttpGet("something")] public async Task<IActionResult> GetSomething(int somethingId) { return await

Is it OK to have virtual async method on base class?

戏子无情 提交于 2020-01-01 00:58:07
问题 I am working with some code, where I have 2 classes with very similar logic and code. I have protected async void LoadDataAsync() method on both classes. Currently I am refactoring it and thinking to move shared logic to base class. Is it OK to have virtual async method on base class and override it on derived classes? Are there any issues with it? My code looks like this: public class Base { protected virtual async void LoadDataAsync() {} } public class Derived : Base { protected override

When to use OrderByCompletion (Jon Skeet) vs Parallel.ForEach with async delegates

别来无恙 提交于 2019-12-31 22:41:52
问题 Recently Jon Skeet at NDC London spoke about C# 5 async/await and presented the idea of " ordering by completion " a list of async tasks. A link http://msmvps.com/blogs/jon_skeet/archive/2012/01/16/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx I am a bit confused or should I say I am not sure when will this technique be more appropriate to use. I cannot understand the difference between this and the below example var bag = new ConcurrentBag<object>(); Parallel.ForEach