Recently I’ve written a lot about using some of the new multi-threading capabilities in .NET 4.5 with the UCMA SDK, and I wanted to put together a complete example in an application that might actually be useful.

I like to take a break every so often to get up from the computer, walk around, and stretch, and I’ve tried a few break reminder applications that give you a pop-up reminder after a period of time. The trouble I always have is that I forget to disable them when I’m on calls, or presenting, and they pop up and get in the way. So I thought I would write a simple app to send IM break reminders, but only when the user is in an Available state in Lync.

I created a C# console app project, targeting .NET Framework 4.5, and changing the platform to x64. I also added a reference to Microsoft.Rtc.Collaboration from the UCMA 4.0 SDK. You can find the code for the whole app here. Once it starts, you send an IM to the app to tell it to start monitoring you, and another IM to tell it to stop.

Let’s start with the extension methods. These allow the code to perform the UCMA operations that I need with the await keyword. Here’s the static class with the extension methods:

There’s a pretty obvious pattern here, so you can probably extrapolate to any other methods you want to create. Any parameters that need to be passed to the Begin method go between the End method and the null at the end (which is for the async state parameter).

With these extension methods in place, I can greatly simplify my UCMA code. The resulting code in the application is a lot more “normal” looking than your average UCMA 3.0 code; it’s easier to read linearly, although it’s still of course executing in the same asynchronous way. Here is the startup and shutdown code, for example:

It’s incredibly awesome to me that we can now write code like this in UCMA applications. Before, this would have involved at least three separate callback methods, six try/catch blocks, and generally much less intuitive code.

2 Comments on “New multi-threading techniques with UCMA 4.0”

[…] make it easier for developers to use the new multithreading techniques from .NET 4.5 in UCMA 4.0 applications, I’ve put together a set of extension methods that turn pairs of Begin/End methods (such as […]