locked
Dispatcher::CurrentDispatcher for C++

Answers

  • Windows::UI::Core:CoreDispatcher is the appropriate dispatcher to use for Windows Store apps (in C++ or in .Net).

    In a Xaml object you can get it from DependencyObject::Dispatcher, or you can cache it yourself if you need to call it on a thread that doesn't have an associated DependencyObject or Window.

    --Rob

    • Marked as answer by rob qqq Wednesday, October 31, 2012 4:43 PM
    Sunday, October 28, 2012 12:47 AM
    Owner
  • Dispatcher::CurrentDispatcher is WPF. It is not relevant for Windows Store apps in either C++ or C#.

    A dispatcher isn't necessary for marshaling between C++ and C#. That transition won't cause a thread change. Your tasks are only marshaled back to the dispatcher thread if they started on it, in which case the dispatcher was already available.

    And again, you can get a dispatcher from a DependencyObject since they have thread affinity to a UI thread. You don't need to be on the thread which has a Window, you just need to have a reference to one.

    --Rob

    • Marked as answer by rob qqq Wednesday, October 31, 2012 4:43 PM
    Tuesday, October 30, 2012 6:18 AM
    Owner

All replies

  • Windows::UI::Core:CoreDispatcher is the appropriate dispatcher to use for Windows Store apps (in C++ or in .Net).

    In a Xaml object you can get it from DependencyObject::Dispatcher, or you can cache it yourself if you need to call it on a thread that doesn't have an associated DependencyObject or Window.

    --Rob

    • Marked as answer by rob qqq Wednesday, October 31, 2012 4:43 PM
    Sunday, October 28, 2012 12:47 AM
    Owner
  • Thanks.

    So C++ could not create a new Dispatcher like C# Dispatcher::CurrentDispatcher. 

    I am curious that how the value of the function with returned type IAsyncOperation<> get marshaled  from C++ to Visual C# background thread in Window8 RT. If the VC# background doesn't have associated Window instance. It creates a dispatcher internally?


    rob qqq

    Monday, October 29, 2012 8:17 PM
  • Dispatcher::CurrentDispatcher is WPF. It is not relevant for Windows Store apps in either C++ or C#.

    A dispatcher isn't necessary for marshaling between C++ and C#. That transition won't cause a thread change. Your tasks are only marshaled back to the dispatcher thread if they started on it, in which case the dispatcher was already available.

    And again, you can get a dispatcher from a DependencyObject since they have thread affinity to a UI thread. You don't need to be on the thread which has a Window, you just need to have a reference to one.

    --Rob

    • Marked as answer by rob qqq Wednesday, October 31, 2012 4:43 PM
    Tuesday, October 30, 2012 6:18 AM
    Owner
  • How would I get a Dispatcher from a background task? My component can also be called from a background task but obviously there is no Window then.
    Tuesday, April 16, 2013 10:15 AM