Despite the fact we can’t currently use it inside AutoCAD – we’re still using .NET 4.0 in AutoCAD 2014 – of late I’ve been working more with the async/await capability in .NET 4.5, which has been really interesting. I’ve worked with asynchronous operations in .NET languages on and off for some time, having started with F#’s Asynchronous Workflows and then spent some time with C#’s async/await after hearing Stephen Toub speak at Microsoft Switzerland a couple of years ago.
It feels like it’s been a while since I did much of that, though: I used the Async CTP sporadically with Visual Studio 2010 – and then used async/await much more extensively when writing apps for Windows 8 – but the fact it’s not been native to the version of the .NET framework used inside AutoCAD has meant I’ve not been using it heavily.
Which also means that some of the information that’s relevant for things I’m looking at now has already been around for a couple of years. For instance, while researching the project I’m working on that uses .NET 4.5 I watched a session Stephen Toub presented at Microsoft’s BUILD conference in 2011 (thanks to Philippe Leefsma for pointing me to this one):
This is an interesting – although quite in-depth – session on async/await. If you’re relatively new to the concepts behind this mechanism, I do suggest looking at some more introductory sessions, first, but this one is definitely worth a watch for anyone who has wondered about the additional work the compiler does when you make use of async/await. And what you can do to improve performance when you need to.
Stephen classes himself a library writer, which means he really cares about low-level system performance. But Stephen’s also pragmatic about how much optimisation needs to be applied to a system – here’s a comment he made a little after the 12-minute mark in this session:
“I always recommend: write your code the way you want to write it. Run your performance tests (everyone should have performance tests). If your performance tests meet your needs/goals, great, you’re done. But if they don’t meet your goals then you need to start thinking through some of these best practices.”
This makes a lot of sense to me. I typically code at the application (rather than library) level and – given the relatively finite amount of time I can allocate to writing blog posts – I tend not to have the chance to focus much on low-level optimisation. It happens from time to time, but not systematically.
This is an important point for readers to note, especially if you want to build code posted here into your own libraries: you really need to make sure it meets the performance requirements for that library – and for your broader application – otherwise you’ll need to optimise it (the chances are it hasn’t been). At some point it might be worth covering some optimisation “best practices” that relate to AutoCAD on this blog… my colleague Albert Szilvasy will be presenting on this at AU2013, so I may well get something from him to share, at some point.
But I digress… back on the subject of async: if you liked the above session, here’s another, more recent one by Stephen Toub (here’s a list of his sessions on Channel 9, in case) that looks at some of how async/await has ended up being used (and things to do or avoid) since it officially became part of .NET: