locked
How to Access UI Element in a Thread Routine?

    Question

  • Hello,

    I have a problem about how to update UI element displaying.

    I'm porting desktop apps to metro style, and I have DLL which has a thread to retrive data from my device, I have passed a Callback funtion address to the DLL from the metro app;When data arrived, the thread will call this callback to notify the Metro apps to display it. Actually, the callback funtion in metro is running in a thread, we can't do any operation for UI in this callback.

    Any suggestion? Thank you.

     

    Tuesday, October 25, 2011 9:27 AM

Answers

  • You will need to find a FrameworkElement on the UI thread and call it's Dispatcher's Invoke method to marshal a callback onto the UI thread.

    --Rob


    Wednesday, October 26, 2011 6:46 PM
    Owner
  • You can find an example of the C++ syntax to get back to the UI thread in the ApplicationData Sample. See the MainPage.xaml.cpp

        // The event handler may be invoked on a background thread, so use the Dispatcher to invoke the UI-related code on the UI thread.
        auto callback = ref new InvokedHandler(
            [_this, text](Object^ sender, InvokedHandlerArgs^ eInvoke) { _this->Scenario4Output_TextBlock->Text = text; },
            CallbackContext::Any
            );
        Scenario4Output_TextBlock->Dispatcher->Invoke(CoreDispatcherPriority::Normal, callback, this, nullptr);
    


    Thanks,

    -David

     

    • Marked as answer by ytshe Wednesday, November 02, 2011 8:01 AM
    Thursday, October 27, 2011 6:21 PM
    Moderator

All replies

  • You will need to find a FrameworkElement on the UI thread and call it's Dispatcher's Invoke method to marshal a callback onto the UI thread.

    --Rob


    Wednesday, October 26, 2011 6:46 PM
    Owner
  • You can find an example of the C++ syntax to get back to the UI thread in the ApplicationData Sample. See the MainPage.xaml.cpp

        // The event handler may be invoked on a background thread, so use the Dispatcher to invoke the UI-related code on the UI thread.
        auto callback = ref new InvokedHandler(
            [_this, text](Object^ sender, InvokedHandlerArgs^ eInvoke) { _this->Scenario4Output_TextBlock->Text = text; },
            CallbackContext::Any
            );
        Scenario4Output_TextBlock->Dispatcher->Invoke(CoreDispatcherPriority::Normal, callback, this, nullptr);
    


    Thanks,

    -David

     

    • Marked as answer by ytshe Wednesday, November 02, 2011 8:01 AM
    Thursday, October 27, 2011 6:21 PM
    Moderator
  • Thank you.
    Thursday, November 10, 2011 5:09 AM
  • In the case where a WinRT creates a thread using the Win32 ::CreateThread API, what are the responsiblities for managing the lifetime of this thread by the thread creator? If the system suspends the app or terminates it should we handle stopping thread or will the system just suspend or terminate it as it sees fit. If this is a critical thread that I wanted to perform some important cleanup for example how should that be managed from the custom WinRT component? Is there a way using the metro libraries that a WinRT component could use to schedule a long running task that could be used instead of creating a worker thread from the WinRT component?

    Thanks

    Sunday, November 27, 2011 9:17 PM
  • Hi,

    I downloaded "ApplicationData Sample" project but no where i found "Dispatcher->Invoke" or "InvokedHandler" in MainPage.xaml.cpp /entire project.

    can you be more elaborate on this.

    I try to use delegate invoke(call back function) from a background thread to update UI element i am unable to update. so i tried to use invoke ex: "progressBarObj->Dispatcher"  but i didn't found "Invoke" function.

    using: VS2012Express for win8 , platform: win32.

    Any suggestion? Thank you.

    Friday, September 28, 2012 2:13 PM