Tag: Tip

How to expose C# async methods in WinRT components

When authoring Windows Runtime (WinRT) components with C# you cannot use the standard C# async Task pattern. Say for example you have code like:

Visual Studio will give you a lengthy error similar to this:

'Demo.MyComponent.SaveToFilel(Windows.Storage.StorageFolder, System.String)' has a parameter of type 'System.Threading.Tasks.Task' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types.
Consider changing the type 'Task' in the method signature to one of the following types instead:
Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces.

The problem is Windows Runtime does not support Task for asynchronous operations. Developers must instead use IAsyncAction or IAsyncOperation. That creates a new problem though; if the function uses await it must be declared with the async keyword, and that requires the function to be return Task!

Thankfully there is a way out of this standoff. Keep the original function but mark it as private. Create a new public method that calls the private implementation and returns the result as an IAsyncAction or IAsyncOperation type with the AsAsyncOperation() method. It’s a bit messy but it works perfectly.

Here’s how it would look for the example above:

Getting rid of C#’s obj folders

A small tip for developers who are irritated with Visual Studio doing this to your pristine source tree:

Why Visual Studio, WHY?!
obj folders mucking up your day

To get the obj directories out of your source tree, and somewhere nicer (your project’s build output directory for example) open up your project’s .csproj file in an external editor. Locate any line that has the <OutputPath> tag on it. On the line below, insert the following:

For example, in my Dungeon Crawler project I have the following in Dungeon Crawler Shared.csproj

Hope that helped!

Printing Generic C++ Containers

Always wanted a simple utility function to print our an arbitrary STL container? Well, so long as your container type support directional iterators… this method I wrote will work perfectly! Its a great example for why templates are awesome

And now how to use the function:

Compiling Qt with Visual Studio

So your awesome project that you’re developing with Visual Studio, and you want to use Trolltech’s Qt library? Luckily Trolltech provides an awesome Visual Studio plugin that does almost everything Qt creator does (minus Intellisense support for slots). The bad news is that Trolltech doesn’t directly tell you how to go about doing this:

  1. Download and install the Qt for Visual Studio plugin.
  2. Download the source code for Qt 4.7
  3. Start the Visual Studio 2010 Command Prompt ( Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt)
  4. Navigate to the directory with your downloaded Qt SDK
  5. Run the configure program like so: “configure -platform win32-msvc2010 -debug-and-release -static -no-gif -no-qt3support -no-dbus -no-phonon-backend”
  6. Wait ten or so minutes for configure to finish
  7. Now run nmake. And make dinner, because this will take a very long time
  8. You have Qt ready to go! Yay!

The configure parameters I provide will configure Visual Studio to build the vast majority of Qt. There’s a lot of extra options you can explore if you desire more control – for instance, you can enable exception support.

Have fun!