Reputation: 6136
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.
Upvotes: 2
Views: 2879
Reputation: 6136
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:
Do not call concurrency::task::wait in the body of a continuation that runs on the STA. Otherwise, the runtime throws concurrency::invalid_operation because this method blocks the current thread and can cause the app to become unresponsive.
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:
class WstringCaselessLess : std::binary_function< std::wstring, std::wstring, bool >
{
public:
bool operator()( const std::wstring& s1, const std::wstring& s2 )
{
return _wcsicmp( s1.c_str(), s2.c_str() ) < 0;
}
};
public ref class ComponentResourceLoader sealed
{
public:
Platform::String^ GetStringResource( Platform::String^ uri )
{
auto key = std::wstring( uri->Data(), uri->Length() );
auto findit = m_resourceMap.find( key );
if ( findit != std::end( m_resourceMap ) )
return ref new Platform::String( findit->second.c_str() );
auto uriObj = ref new Windows::Foundation::Uri( uri );
auto fileOp = Windows::Storage::StorageFile::GetFileFromApplicationUriAsync( uriObj );
return concurrency::create_task( fileOp )
.then( [this, &key]( Windows::Storage::StorageFile^ file )
-> Windows::Foundation::IAsyncOperation< Windows::Storage::Streams::IBuffer^ >^
{
return Windows::Storage::FileIO::ReadBufferAsync( file );
} )
.then( [this, &key]( Windows::Storage::Streams::IBuffer^ buffer )
-> Platform::String^
{
auto reader = Windows::Storage::Streams::DataReader::FromBuffer( buffer );
auto str = reader->ReadString( buffer->Length );
this->m_resourceMap[key] = std::wstring( str->Data(), str->Length() );
return str;
} ).get();
}
private:
std::map< std::wstring, std::wstring, WstringCaselessLess > m_resourceMap;
};
The broken button click handler looks like this:
private void WinRT_Click_1(object sender, RoutedEventArgs e)
{
TextContent.Text = m_loader.GetStringResource(@"ms-appx:///Assets/Hello.xml");
}
If I change the button handler to load the string in a separate thread, it works:
private async void WinRT_Click_1(object sender, RoutedEventArgs e)
{
var text = await Task.Run<string>(() => RunWinrtLoader());
TextContent.Text = text;
}
private string RunWinrtLoader()
{
return m_loader.GetStringResource(@"ms-appx:///Assets/Hello.xml");
}
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.
Upvotes: 5