I have been experimenting a lot lately with Windows 8, writing C# XAML Metro style apps using WinRT components written in C++/CX for better performance and to use functionality not available in C#, specifically DirectX.
While loading resources from my app package in my WinRT component, my app threw the following:
Unhandled exception at 0x0fd58ae3 (MSVCR110D.dll) in TestResources.exe: 0xC0000409: 0xc0000409.
I was trying to make asynchronous calls to the new StorageFile API (Windows::Storage::StorageFile::GetFileFromApplicationUriAsync chained to other calls for reading the file contents) and synchronize the resulting task chain using concurrency::task.get().
It didn’t seem to be working. If I didn’t call concurrency::task.get() or concurrency::task.wait(), then the problem did not occur, but I needed the result synchronously because of how my DirectX code was written.
The reason is that you are calling concurrency::task.wait() or concurrency::task.get() from the UI thread! The framework throws an exception to prevent you from freezing the app. See: Creating Asynchronous Operations in C++ for Metro style Apps, toward the bottom there are three warnings. The last warning says:
I wrote a test app and verified that I could make everything work by calling my WinRT component from a separate thread!
Details below:
My test app is C# XAML Metro app calling WinRT component to load a string from a file. It has a Button and a TextBlock.
The resource loader looks like this:
The broken button click handler looks like this:
If I change the button handler to load the string in a separate thread, it works:
Hope this is helpful to someone! It sure had me pretty angry for a while, because there is no indication from the error to the real problem.