【发布时间】:2012-05-10 08:48:19
【问题描述】:
如果我尝试访问任务(和 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!
});
}
如果我们添加 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());
【问题讨论】:
-
你遇到了什么异常?
-
问题已更新 - VS11 调试器报告:Concurrency::unobserved_task_exception
-
那不多说了,是不是有内部异常?
-
尝试在 Dispatcher 线程上调用它,如果有的话。
-
异常:应用程序调用了一个为不同线程编组的接口。
标签: xaml windows-runtime winrt-xaml c++-cx