Async ShowDialog

It’s easy to implement with Task.Yield, like below (WinForms, no exception handling for simplicity). It’s important to understand how the execution flow jumps over to a new nested message loop here (that of the modal dialog) and then goes back to the original message loop (that’s what await progressFormTask is for): namespace WinFormsApp { internal … Read more

Do I need to be concerned with race conditions with asynchronous Javascript?

All Javascript event handler scripts are handled from one master event queue system. This means that event handlers run one at a time and one runs until completion before the next one that’s ready to go starts running. As such, there are none of the typical race conditions in Javascript that one would see in … Read more

Asynchronous File Download with Progress Bar

UI Thread will be freezed when you click startDownload(). If you don’t want get form freezed, you use startDownload() in another thread and make progress updating in cross-threaded. One way, private void startDownload() { Thread thread = new Thread(() => { WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.DownloadFileAsync(new … Read more

A pattern to pause/resume an async task?

Updated for 2019, I’ve recently had a chance to revisit this code, below is complete example as a console app (warning: PauseTokenSource needs good unit testing). Note, in my case, the requirement was that when the consumer-side code (which requested the pause) would continue, the producer-side code should have already reached the paused state. Thus, … Read more

Is the “async” attribute/property useful if a script is dynamically added to the DOM?

The question is does s.async = true have a use for dynamically inserted scripts, or are these loaded asynchronously already. The answer is they aren’t loaded asynchronously in all browsers, as explained here (thanks to Markus Olsson for the link) script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox. … Read more

Asynchronous iterator Task

A more “batteries-included” implementation of this kind of thing, including language support, is now available as of C# 8.0. Now, when using at least C# 8.0 (or higher) with .NET Standard 2.1 (or higher) and/or .NET Core 3.0 (or higher), the code from the original question may be written as follows: private async IAsyncEnumerable<char> TestAsync(string … Read more

Is async/await suitable for methods that are both IO and CPU bound?

There are two good answers already, but to add my 0.02… If you’re talking about consuming asynchronous operations, async/await works excellently for both I/O-bound and CPU-bound. I think the MSDN docs do have a slight slant towards producing asynchronous operations, in which case you do want to use TaskCompletionSource (or similar) for I/O-bound and Task.Run … Read more

What happens while waiting on a Task’s Result?

In Windows, all I/O is asynchronous. Synchronous APIs are just a convenient abstraction. So, when you use HttpWebRequest.GetResponse, what actually happens is the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting for it to complete. Similarly, when you use HttpClient.PostAsync(..).Result, the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting … Read more