2012-10-12 1 views
0
auto task = create_task(Windows::Storage::KnownFolders::PicturesLibrary->GetFilesAsync(Windows::Storage::Search::CommonFileQuery::OrderBySearchRank)); 

task.then([&sstrpath](Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile^>^ files) 
{ 
    CCLog("num of files detected %d",files->Size); 
    Platform::String^ pathstr = files->GetAt(0)->Path; 
    OutputDebugStringW(pathstr->Data()); 

    auto task2 = create_task(files->GetAt(0)->OpenAsync(Windows::Storage::FileAccessMode::Read)); 

    task2.then([](Windows::Storage::Streams::IRandomAccessStream^ filestream) 
    { 
     Windows::UI::Xaml::Media::Imaging::BitmapImage^ bmp = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(); 
     bmp->SetSource(filestream); 
    } 
    ); 
} 
); 

이것은 C++ (cocos2dx 프레임 워크)의 VS express RTM이있는 Win8 RTM에서 수행되었습니다. 그림 라이브러리에서 이미지를로드하고 그 중 BitmapImage를 만들려고합니다. 다음은 cococs2dx에서 BitmapImage를 CCSprite 용으로 사용하는 것이었지만 여기서는 문제가 아닙니다. 이 프로그램은 task2로 진행할 수 있었지만 BitmmapImage를 새로 참조하려고 할 때 충돌이 발생했습니다. 다음은 내가 Win8 응용 프로그램 개발을위한 기반 자바 스크립트 또는 XAML 거기에서 내가 가장 튜토리얼로 잘못 한 일을 정말 잘 모르겠습니다 출력 콘솔KnownFolders에서 BitmapImage로 이미지로드

First-chance exception at 0x75004B32 in myGame.exe: Microsoft C++ exception:   
Platform::WrongThreadException^at memory location 0x02D1D794. HRESULT:0x8001010E 
First-chance exception at 0x75004B32 in myGame.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000. 
First-chance exception at 0x75004B32 in myGame.exe: Microsoft C++ exception: 
Platform::WrongThreadException^at memory location 0x02D1E5F0. HRESULT:0x8001010E 
Unhandled exception at 0x622C9AD1 (msvcr110d.dll) in PixBlitz.exe: An invalid parameter was passed to a function that considers invalid parameters fatal. 

했다.

+0

앱에 해당 파일 위치 및 파일 형식에 대한 액세스 권한이 있습니까? – mydogisbox

+0

예, 그렇지 않으면 실행이 첫 번째 태스크를 통과하지 못했을 것입니다. –

답변

0

이와 같은 작업을 생성하면 코드를 다른 스레드로 옮길 수 있습니다. 잘못된 스레드에서 객체에 액세스하면 Platform :: WrongThreadException이 발생합니다. 매번 동일한 스레드에서 GUI 구성 요소에 액세스해야합니다.

당신이 Dispatcher에 RunAsync를 걸면 스레딩이 올바르게 수행 될 것입니다. 대리자를 RunAsync에 전달하고 해당 대리자가 BitmapImage 만들기 및 BitmapImage로 수행 할 작업을 포함하도록합니다.


요청한대로.

필자는 Win8 개발에 익숙하지 않지만, 필자가 기반으로하는 WPF에 익숙합니다. (C++/CLI가 람다를 지원한다면, 나는 그 주제에 대한 옛 대답을 다시 고쳐야 할 것이다.)

나는 이것이 당신이 원하는 것이라고 생각한다. 그 대리인에 대한 약간의 구문을 약간 가지고있을 수 있습니다.

Stream^ filesteam = files->GetAt(0)->Open(FileAccessMode::Read); // we're already on a background thread, no need for a Task if we're not going to call 'then'. 

// I'm assuming 'this' is a subclass of Windows.UI.Xaml.Window, or something similar. 
this->Dispatcher->RunAsync(CoreDispatcherPriority::Low, []() 
{ 
    BitmapImage^ bmp = ref new BitmapImage(); 
    bmp->SetSource(filestream); 
} 
); 
+0

데비드의 답변 주셔서 감사합니다. 그러나 당신은 나를 잃어 버렸습니다. 나는 꽤 cocos2d (-x)로 작업하는 데 익숙하며 Win8 개발 라이브러리에 익숙하지 않다. 예를 들어 설명해 주실 수 있습니까? –

+0

저는 Win8 개발에 익숙하지 않습니다. 그러나 WPF를 기반으로합니다. 이 코드를 사용해보십시오. –