0

タスク (および 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());
4

1 に答える 1

0

UI/ディスパッチャースレッド以外のスレッドからUI要素を呼び出しています。control.Dispatcher.InvokeAsync()を使用してUI要素のメソッドを呼び出すか、バックグラウンドスレッドから呼び出していないことを確認する必要があります。

于 2012-05-10T16:27:35.297 に答える