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

Directx 在异步方法上使用WinRT组件调用task.wait()或task.get()的C#XAML Metro应用程序引发:未处理的异常0xc0000409,directx,microsoft-metro,windows-runtime,winrt-xaml,c++-cx,Directx,Microsoft Metro,Windows Runtime,Winrt Xaml,C++ Cx,最近,我在Windows8上做了很多实验,使用C++/CX编写的WinRT组件编写C#XAML Metro风格的应用程序,以获得更好的性能,并使用C#中没有的功能,特别是DirectX 从我的WinRT组件中的我的应用程序包加载资源时,我的应用程序抛出以下错误: TestResources.exe中0x0fd58ae3(MSVCR110D.dll)处未处理的异常:0xC0000409:0xC0000409 我试图对新的StorageFile API进行异步调用(Windows::Storage:

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

从我的WinRT组件中的我的应用程序包加载资源时,我的应用程序抛出以下错误:

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

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


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

原因是您正在从UI线程调用concurrency::task.wait()或concurrency::task.get()!框架抛出一个异常以防止您冻结应用程序。请参阅:,底部有三个警告。最后一条警告说:

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

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

详情如下:

我的测试应用程序是C#XAML Metro应用程序,它调用WinRT组件从文件中加载字符串。它有一个按钮和一个文本块

资源加载程序如下所示:

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;
};
private void WinRT_Click_1(object sender, RoutedEventArgs e)
{
    TextContent.Text = m_loader.GetStringResource(@"ms-appx:///Assets/Hello.xml");
}
如果我将按钮处理程序更改为在单独的线程中加载字符串,它将起作用:

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");
}
private async void WinRT\u Click\u 1(对象发送方,RoutedEventArgs e)
{
var text=wait Task.Run(()=>RunWinrtLoader());
Text=Text;
}
私有字符串RunWinrtLoader()
{
返回m_loader.GetStringResource(@“ms-appx:///Assets/Hello.xml");
}
希望这对别人有帮助!这确实让我生气了一段时间,因为从错误到真正的问题没有任何迹象