What’s the point of await DoSomethingAsync [closed]

The result of both calls is the same.

The difference is that var stream = file.readAsStream() will block the calling thread until the operation completes.

If the call was made in a GUI app from the UI thread, the application will freeze until the IO completes.

If the call was made in a server application, the blocked thread will not be able to handle other incoming requests. The thread pool will have to create a new thread to ‘replace’ the blocked one, which is expensive. Scalability will suffer.

On the other hand, var stream = await file.readAsStreamAsync() will not block any thread. The UI thread in a GUI application can keep the application responding, a worker thread in a server application can handle other requests.

When the async operation completes, the OS will notify the thread pool and the rest of the method will be executed.

To make all this ‘magic’ possible, a method with async/await will be compiled into a state machine. Async/await allows to make complicated asynchronous code look as simple as synchronous one.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)