Examining Async Behavior in .NET Notebooks

This post was written and published as part of the 2021 C# Advent.

One of my favorite new tools is the .NET Interactive Notebooks plugin for Visual Studio Code. It allows you to intermingle executable code and markdown descriptions of it. So you can explain the code, run the code, and see its results all in one place. This makes the notebooks incredibly useful for teaching and exploring concepts.

One nice feature of Notebooks is that the display function returns an object that can update the displayed value. So unlike console functions that display a list of updated values, Notebooks allow you to update values in place.

I wanted to know what Notebooks could do with asynchronous code. So I created two simple methods that code runs for a long enough period that their asynchronous behavior would be obviously noticeable. Both functions display their current activity and continuously update it in place.

Then I invoke both functions, assign their tasks into variables, and then use Task.AwaitAll to wait until both functions run to completion. When the code is executed, you can see that both functions are executing at the same time. (Yes, I know that they aren’t necessarily running simultaneously at the CPU level.)

Now that we understand the Notebook behavior a little better, we can run some experiments. What happens when we square all the values from 1 to 100 but only cube the values from 1 to 50? We see that the cube function finishes much earlier. What happens when we change the WhenAll function to WhenAny? We see that the squares function stops executing when the cube function completes.

The beautiful part of working with .NET notebooks is that you can experiment with the code right in the notebook. I encourage you to head over to the GitHub repo to download the notebook and try it yourself.

December 14, 2021 |
Tags : C#

Comments Section

Feel free to comment on the post but keep it clean and on topic.

comments powered by Disqus

About Me

Eric Potter My name is Eric Potter. I have an amazing wife and 5 wonderful children. I am a Microsoft MVP for Developer Tools and Technologies, a Principal Architect for Core BTS in Ft. Wayne Indiana, and an adjunct professor for Indiana Tech. I am a humble toolsmith.

Microsoft MVP Award

pottereric.github.com