使用WinRT组件的C#XAML Metro应用程序在异步方法上调用task.wait()或task.get()抛出:未处理的异常0xc0000409

pet*_*snd 2 directx microsoft-metro windows-runtime c++-cx winrt-xaml

我最近在Windows 8上进行了很多实验,使用用C++/CX编写的WinRT组件编写C#XAML Metro风格的应用程序,以获得更好的性能,并使用C#中没有的功能,特别是DirectX.

在我的WinRT组件中从我的应用包中加载资源时,我的应用程序抛出了以下内容:

TestResources.exe中0x0fd58ae3(MSVCR110D.dll)的未处理异常:0xC0000409:0xc0000409.

我试图对新的StorageFile API进行异步调用(Windows :: Storage :: StorageFile :: GetFileFromApplicationUriAsync链接到其他调用来读取文件内容)并使用concurrency :: task.get()同步生成的任务链.

它似乎没有起作用.如果我没有调用concurrency :: task.get()或concurrency :: task.wait(),那么问题就不会发生,但由于我的DirectX代码的编写方式,我需要同步结果.

pet*_*snd 5

原因是你从UI线程调用concurrency :: task.wait()或concurrency :: task.get()!框架抛出异常以防止您冻结应用程序.请参阅:使用C++为Metro风格的应用程序创建异步操作,底部有三个警告.最后一个警告说:

不要在STA上运行的延续体中调用concurrency :: task :: wait.否则,运行时会抛出concurrency :: invalid_operation,因为此方法会阻止当前线程,并可能导致应用程序无响应.

我写了一个测试应用程序并验证我可以通过从一个单独的线程调用我的WinRT组件来使一切正常工作!

详情如下:

我的测试应用程序是C#XAML Metro应用程序调用WinRT组件从文件加载字符串.它有一个Button和一个TextBlock.

资源加载器如下所示:

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;
};
Run Code Online (Sandbox Code Playgroud)

破坏的按钮单击处理程序如下所示:

private void WinRT_Click_1(object sender, RoutedEventArgs e)
{
    TextContent.Text = m_loader.GetStringResource(@"ms-appx:///Assets/Hello.xml");
}
Run Code Online (Sandbox Code Playgroud)

如果我更改按钮处理程序以在单独的线程中加载字符串,它可以工作:

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");
}
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助!它肯定让我生气了一段时间,因为没有迹象表明错误是真正的问题.