locked
progress ring and tasks

    Question

  • Hi,

    I am trying do display a progress ring while a task does some heavy computation but when I try to deactivate the progress ring it'll give me a WrongThreadException. So I guess I can't update UI elements form a different thread so what is the correct way of doing this:

    progressRing->IsActive = true;
    progressRing->Visibility = Windows::UI::Xaml::Visibility::Visible;
    
    auto evaluationTask = Concurrency::create_task([this]
    {
    
    // ... Do lot's of computation
    
    progressRing->IsActive = false;
    progressRing->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    });

    Wednesday, October 9, 2013 12:19 PM

Answers

  • I didn't test it but i think the code should be as following:

    progressRing->IsActive = true;
    progressRing->Visibility = Windows::UI::Xaml::Visibility::Visible;
    auto evaluationTask =  create_task([this](){
       //... Do lot's of computation                                
     }).then([=]()
     {
                progressRing->IsActive = false;
                progressRing->Visibility =Windows::UI::Xaml::Visibility::Collapsed;                                                                                                                  
     },task_continuation_context::use_current());

    use use_current to make sure that the UI operation is executed in the current thread which is UI thread.

    Best Regards,

    Han Xia


    My Blog:http://blogs.msdn.com/b/hanxia

    • Marked as answer by ch.b Wednesday, October 9, 2013 3:40 PM
    Wednesday, October 9, 2013 3:16 PM

All replies

  • I didn't test it but i think the code should be as following:

    progressRing->IsActive = true;
    progressRing->Visibility = Windows::UI::Xaml::Visibility::Visible;
    auto evaluationTask =  create_task([this](){
       //... Do lot's of computation                                
     }).then([=]()
     {
                progressRing->IsActive = false;
                progressRing->Visibility =Windows::UI::Xaml::Visibility::Collapsed;                                                                                                                  
     },task_continuation_context::use_current());

    use use_current to make sure that the UI operation is executed in the current thread which is UI thread.

    Best Regards,

    Han Xia


    My Blog:http://blogs.msdn.com/b/hanxia

    • Marked as answer by ch.b Wednesday, October 9, 2013 3:40 PM
    Wednesday, October 9, 2013 3:16 PM
  • Excellent, thank you :).
    Wednesday, October 9, 2013 3:40 PM