Exception accessing XAML controls inside a task
-
10 พฤษภาคม 2555 8:37
If I try to access XAML controls inside a task (and task::then) my Metro XAML app always stops with an exception.
The same code works without any problems outside the task. I didn't find any answer - what did I miss?VS11 Debugger reports: Concurrency::unobserved_task_exception
Exception: The application called an interface that was marshalled for a different thread.
Many thanks for your help!
void MyClass::MyMemberFunction()
{
xamlStoryboard->Stop(); // ok
xamlImage->Source = ref new BitmapImage(); // oktask<void> atask([this] ()
{
xamlStoryboard->Stop(); // exception!
xamlImage->Source = ref new BitmapImage(); //exception!
});atask.then([this] ()
{
xamlStoryboard->Stop(); // exception!
xamlImage->Source = ref new BitmapImage(); //exception!
});
}- แก้ไขโดย Tobias Elestes 10 พฤษภาคม 2555 9:08
- แก้ไขโดย Tobias Elestes 10 พฤษภาคม 2555 10:43
- แก้ไขโดย Tobias Elestes 10 พฤษภาคม 2555 10:53
- แก้ไขโดย Tobias Elestes 10 พฤษภาคม 2555 11:05
- แก้ไขโดย Tobias Elestes 10 พฤษภาคม 2555 11:10
ตอบทั้งหมด
-
11 พฤษภาคม 2555 5:21ผู้ดูแล
Hello,
How about task<void> atask([this, xamlStoryboard] (){}?
As far as I know, the this pointer to the capture clause to provide access to the methods and data members of the enclosing class.
So where do you define the xamlStoryboard
Best regards,
Jesse
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
-
11 พฤษภาคม 2555 9:08
Hello Jesse,
xamlStoryboard is defined in MyClass.xaml
task<void> atask([this, xamlStoryboard] (){}VS11 Error: Member MyClass::xamlStoryboard is not a variable
But I found:Creating Asynchronous Operations in C++ for Metro style Apps
http://msdn.microsoft.com/en-us/library/hh750082(v=vs.110).aspxChapter "Controlling the Execution Thread" explains, why it isn't possible to directly access XAML controls from inside the task.
Maybe I can use the controls dispatcher?
But I further don't unterstand, why I get exceptions in the task::then continuation code. I thought task::then is the callback code executed after the task finishes and runs in the main thread with access to UI/XAML controls?best regards,
Tobias -
11 พฤษภาคม 2555 10:33
If you pass task_continuation_context::use_current() as a second parameter to the 'then' method then your callback will be scheduled as expected without needing to use the Dispatcher.
For further info see Asynchronous programming in C++ about 2/3 of the way down in 'Managing the thread context'.
- ทำเครื่องหมายเป็นคำตอบโดย Tobias Elestes 11 พฤษภาคม 2555 11:07
-
11 พฤษภาคม 2555 11:17
Thanks BabaAndThePigman for the solution!
Working code is (see postings and links for details):
void MyClass::MyMemberFunction()
{
xamlStoryboard->Stop(); // ok
xamlImage->Source = ref new BitmapImage(); // oktask<void> atask([this] ()
{
xamlStoryboard->Stop(); // exception!
xamlImage->Source = ref new BitmapImage(); //exception!
});atask.then([this] ()
{
xamlStoryboard->Stop(); // ok
xamlImage->Source = ref new BitmapImage(); // ok
}, task_continuation_context::use_current()); // that parameter SOLVED the described problems!
}