locked
How to update ui (XAML element) after ppl tasks completed

    Question

  • Hi there, title sais all.

    Below sample code generates "Unhandled exception at 0x60C09AD1 (msvcr110d.dll) in ******: An invalid parameter was passed to a function that considers invalid parameters fatal.", when reaches the Append. And in general what is the best practice to  update ui thread after ppl tasks got completed?

    concurrent_vector<int> vec;

    concurrent_vector<task<void>> tasks;

    for (int i = 0; i<200; ++i){

    tasks.push_back(

    task<void>([&vec,i]{vec.push_back(i);}));

    }

    when_all(begin(tasks),end(tasks)).then([

    this,&vec](){

    this->MyListView->Items->Append(vec.size());

    });


    • Edited by NubCoder Monday, November 19, 2012 10:41 AM
    Monday, November 19, 2012 10:39 AM

Answers

  • I think current() means current thread when the task is scheduled. It will be UI thread if code is called by user interaction. But may be other thread.

    I didn't find a good way to explicit ask for UI thread. Maybe I can save the value of current() in UI thread and use it later? (I assume the value is just a number indicating the thread, and I also assume that number won't change).

    • Marked as answer by NubCoder Friday, November 23, 2012 8:52 AM
    Wednesday, November 21, 2012 3:21 AM

All replies

  • found the answer myself, using Chanel9 (video file: AsyncMadeSimplewithCPPL).

    After last .then in order to schedule bk to main UI thread need to specify continuation context, so last line of the code above becomes:

    },

    task_continuation_context::use_current());

    Hope that will help other nabs like me, messing around due to lack of ANY C++/CX book or online training out there.

    • Marked as answer by NubCoder Monday, November 19, 2012 8:07 PM
    • Unmarked as answer by NubCoder Wednesday, November 21, 2012 7:55 AM
    • Proposed as answer by Ciprian Stanciu Tuesday, January 29, 2013 6:23 AM
    Monday, November 19, 2012 8:07 PM
  • Thanks for sharing the solution.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, November 21, 2012 2:30 AM
  • I think current() means current thread when the task is scheduled. It will be UI thread if code is called by user interaction. But may be other thread.

    I didn't find a good way to explicit ask for UI thread. Maybe I can save the value of current() in UI thread and use it later? (I assume the value is just a number indicating the thread, and I also assume that number won't change).

    • Marked as answer by NubCoder Friday, November 23, 2012 8:52 AM
    Wednesday, November 21, 2012 3:21 AM
  • I think current() means current thread when the task is scheduled. It will be UI thread if code is called by user interaction. But may be other thread.

    I didn't find a good way to explicit ask for UI thread. Maybe I can save the value of current() in UI thread and use it later? (I assume the value is just a number indicating the thread, and I also assume that number won't change).

    You are correct - ::use_current() might mean "run on current thread, when you schedule (or when started ?) task;". If someone knows how to arbitrary and to explicitly specify the UI thread (smtg like ,task_continuation_context::use_UI_thread();), or other way around, that would help tons.

    Anyone advanced enough to help with this?

    Wednesday, November 21, 2012 7:57 AM
  •  If someone knows how to arbitrary and to explicitly specify the UI thread (smtg like ,task_continuation_context::use_UI_thread();), or other way around, that would help tons.

    Anyone advanced enough to help with this?

    It`s impossible, as there can be several UI threads (one per view), and you must specify which one you need.

    auto ui_ctx = task_continuation_context::use_current() captures context of the thread where it is called, and you should pass this ui_ctx to be able to run continuation on this specific UI thread.


    Friday, May 22, 2015 9:15 AM