タスク (および task::then) 内の XAML コントロールにアクセスしようとすると、Metro XAML アプリは常に例外で停止します。同じコードは、タスク以外では問題なく機能します。答えが見つかりませんでした - 何を見逃したのですか?
VS11 デバッガー レポート: Concurrency::unobserved_task_exception
例外: アプリケーションは、別のスレッド用にマーシャリングされたインターフェイスを呼び出しました。
助けてくれて本当にありがとうございます!
void MyClass::MyMemberFunction()
{
xamlStoryboard->Stop(); // ok
xamlImage->Source = ref new BitmapImage(); // ok
task<void> atask([this] ()
{
xamlStoryboard->Stop(); // exception!
xamlImage->Source = ref new BitmapImage(); //exception!
});
atask.then([this] ()
{
xamlStoryboard->Stop(); // exception!
xamlImage->Source = ref new BitmapImage(); //exception!
});
}
2 番目のパラメーターとして task_continuation_context::use_current() を追加すると、atask.then() 継続コードは例外なく実行されます。
atask.then([this] ()
{
xamlStoryboard->Stop(); // now ok!
xamlImage->Source = ref new BitmapImage(); // now ok!
}, task_continuation_context::use_current());