Live data from Hacker News

Asynchronous Programming in C#

github.com

111–120 of 186 posts

Re: Asynchronous Programming in C#

#111
post #26

>Prefer async/await over directly returning Task This one seems questionable to me. I've never been bitten by any of the cons mentioned[1], and it's even noted that doing it this way does incur performance costs. I've learned over the years that if the code path is very prolific, it pays to avoid the async state machine. I'm curious if others could expand on this one. [1] https://github.com/davidfowl/AspNetCoreDiagno…

Always using async/await is recommended to avoid _surprising_ behavior. If a method with a signature Task Foo(); and it is not declared with async and it throws an exception, the exception is propagated directly to the call site. Think of this usage: var getBarTask = Foo(); // do some other stuff try { var bar = await getBarTask; } catch (Exception ex) { handle exceptions } Then if the Foo is not async the exception…

It's actually the case that's marked async that surprises me more. But I don't think the difference has ever mattered in code that I've written or worked with.

The reason that the non-async case makes sense to me is that I know there's usually going to be some synchronous code execution before the function I'm calling has to go async. And in that case I expect the code the executed before going async to come up the stack where I called it instead of where I'm awaiting it. And of course I expect exceptions beyond that to only be able to be retrieved when I await the task since the call stack will be rooted in the event loop after going async.

Re: Asynchronous Programming in C#

#112

The sync over async issue is a real common problem for me when trying to get a large older codebase converted to async and you can't just do it all at once. You still need support non async callers and you want to share code between the new async version and the old sync versions it makes it really difficult to do so. Say you have a db layer you want to move to async but you still have to support a sync api over that…

Database operations should be async, you shouldn't consume them synchronously, as they do IO. Async/Await came out in 2012, almost a decade ago (and with great first party library support, I might add). Moralizing aside, sometimes you do want to call async APIs as synchronous code. I don't think there should be a synchronous version of the API implemented as well, you just need to do var myValue = DoSomethingAsync().…

There's plenty of database operations I don't do async. We heavily make use of ETLs. By their nature those processes are very linear and don't benefit at all from being async.

Re: Asynchronous Programming in C#

#114

Earlier quoted context omitted.

If task A & B perform IO (eg, a DB call) and the alternatives are running them sequentially on one thread or running them concurrently (via async/await) on one thread, then running them concurrently can both decrease end-to-end latency and increase throughput. > If you just synchronously run A then B, the overall time would be shorter (higher throughput) because of less context switching overhead. There are no contex…

> There are no context switches: async/await isn't threads. By "context switch", I didn't meant to imply "hardware thread context switch", just the general sense of "spend some CPU time messing about with scheduling". There is overhead to async in that you're unwinding the stack, bouncing to the thread pool scheduler, loading variables from the heap (since your async code was compiled to closures) back onto the stack…

[deleted]

Re: Asynchronous Programming in C#

#115
post #113

Sorry for OT but whats up with those camelcase method names starting with upper case letter? Very weird convention to my eyes

Not to get too deep into a bikeshed conversation but I'm pretty sure thats is normal c# convention [1].

[1] https://docs.microsoft.com/en-us/dotnet/standard/design-guid...

Re: Asynchronous Programming in C#

#118
post #32

I hope the author at some point adds the section on ConfigureAwait. I've seen code bases where the devs have added .ConfigureAwait(false) to all invokations "just to make sure".

In an event loop model, I've never felt the need to reach for ConfigureAwait(false). Maybe there's certain operations that could be sped up a bit by letting them resume on any thread, but generally I want to be sure that the event loop is executing my code. There wouldn't be much of a point to using an event loop if nothing ever returned back to executing on it.

Re: Asynchronous Programming in C#

#119

Earlier quoted context omitted.

Sure, but I'd argue that the best way to handle this is to just stick to the already existing paradigms that developers have experience with. It's not like there are any problems which can't be solved without async or tpl, and it's not like Microsoft wants to introduce a similar paradigm outside of the .net ecosystem, so it's just creating an artificial barrier and making the code harder to understand so that they ca…

JavaScript/TypeScript and C++ do implement a similar paradigm, loosely patterned on what C# does?

Python and Rust as well. C# invented a large part of the design of async/await that has been adopted by other languages. I figure if there's a better way, it would have been improved on by now since those other languages had plenty of time to see the issues with C#'s version.

Re: Asynchronous Programming in C#

#120
post #108

Earlier quoted context omitted.

This is so true. .NET has been steadily going downhill for some time now. The best indicator is the absolutely rotten documentation for the more recent .NET stuff. Compare that to the older .NET Framework and/or Winapi documentation which was excellent.

It is fighting in a space which is very competitive. Java, Go, JavaScript and Python. The later two are being favorite of every university or coding camp graduate. You stay relevant or you die. Unfortunately, that implies faster dev cycles and areas like docs which are not well served.

How is .NET fighting against JavaScript or Python? Java or Go, I can understand but not JavaScript or Python.
Post reply on HN