locked
Update UI after a task completes or a threadpool completes (Metro style app)

    Question

  • I want to run a new thread in a for loop.

    foreach (StorageFile file in files) { IAsyncAction action = ThreadPool.RunAsync(new WorkItemHandler((IAsyncAction source) => { LocalEpubBook(bookFolder, reader, b, bookInfo, file); })); action.Completed = new AsyncActionCompletedHandler(async (IAsyncAction source, AsyncStatus statua)

    => { await addBookToShelf(file.Name, b); }); }

    In addBookToShelf, the app will update binding the variable of UI control. So it turns out a exception. when it is running. How do I update UI in the complete function?(I want to get a result from the thread, so I must do something in complete function.) Thanks!

    (I try to task api  instead, but the  app must await the task ,so there is only one thread at the same time, I want the app can run some threads at the same time);


    Monday, June 4, 2012 7:49 AM

Answers

  • You are probably getting a cross thread exception? (wrong thread??) I think you need to use the Dispatcher to actually merge back to the main ui thread. You might need to use something like this:

    action.Completed = new AsyncActionCompletedHandler((IAsyncAction source, AsyncStatus status) =>
    { //await addBookToShelf(file.Name, b);
        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            addBookToShelf(file.Name, b);
        });
    });


    Can Bilgin
    Blog CompuSight


    • Edited by Can Bilgin Monday, June 4, 2012 11:09 AM code edit
    • Proposed as answer by Can Bilgin Tuesday, June 5, 2012 2:57 PM
    • Marked as answer by hilda.chen Wednesday, June 13, 2012 8:28 AM
    Monday, June 4, 2012 9:12 AM
  • If your question has been answered, please mark the appropriate post as answer so other community members with a similar question can find help faster.

    Can Bilgin
    Blog CompuSight

    • Marked as answer by hilda.chen Wednesday, June 13, 2012 8:28 AM
    Sunday, June 10, 2012 4:45 PM

All replies

  • You are probably getting a cross thread exception? (wrong thread??) I think you need to use the Dispatcher to actually merge back to the main ui thread. You might need to use something like this:

    action.Completed = new AsyncActionCompletedHandler((IAsyncAction source, AsyncStatus status) =>
    { //await addBookToShelf(file.Name, b);
        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            addBookToShelf(file.Name, b);
        });
    });


    Can Bilgin
    Blog CompuSight


    • Edited by Can Bilgin Monday, June 4, 2012 11:09 AM code edit
    • Proposed as answer by Can Bilgin Tuesday, June 5, 2012 2:57 PM
    • Marked as answer by hilda.chen Wednesday, June 13, 2012 8:28 AM
    Monday, June 4, 2012 9:12 AM
  • If your question has been answered, please mark the appropriate post as answer so other community members with a similar question can find help faster.

    Can Bilgin
    Blog CompuSight

    • Marked as answer by hilda.chen Wednesday, June 13, 2012 8:28 AM
    Sunday, June 10, 2012 4:45 PM