CoreDispatcher.RunAsync method

Applies to Windows and Windows Phone

Schedules the provided callback on the UI thread from a worker thread, and returns the results asynchronously.

Syntax


public:
IAsyncAction^ RunAsync(
  CoreDispatcherPriority priority, 
  DispatchedHandler^ agileCallback
)

Parameters

priority

Type: CoreDispatcherPriority

Specifies the priority for event dispatch. Set this to CoreDispatcherPriority::Normal.

agileCallback

Type: DispatchedHandler

The callback on which the dispatcher returns when the event is dispatched.

Return value

Type: IAsyncAction

The object that provides handlers for the completed async event dispatch.

Remarks

If you are on a worker thread and want to schedule work on the UI thread, use CoreDispatcher::RunAsync. Always set the priority to CoreDispatcherPriority::Normal or CoreDispatcherPriority::Low, and ensure that any chained callbacks also use CoreDispatcherPriority::Normal or CoreDispatcherPriority::Low.

If you are porting from the Microsoft .NET Framework Dispatcher.BeginInvoke and Dispatcher.Invoke methods, note that CoreDispatcher::RunAsync is asynchronous. There is no synchronous version. After you change Dispatcher.Invoke to CoreDispatcher::RunAsync, your code must support the Windows Runtime async pattern and use the specific lambda syntax for your chosen language.

The following examples demonstrate the use of CoreDispatcher::RunAsync to schedule work on the main UI thread using the CoreWindow's event dispatcher.

To spin off a worker thread from the UI thread, do not use this method (CoreDispatcher::RunAsync). Instead, use one of the Windows::System::Threading::ThreadPool::RunAsync method overloads.

Note  Callbacks scheduled with CoreDispatcherPriority::Low priority are called when there are no pending input events. Use the CoreDispatcherPriority::Low priority to make your app UI more responsive. To schedule background tasks, use CoreDispatcher::RunIdleAsync.



// 
_dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, 
                     ref new Windows::UI::Core::DispatchedHandler([this]()
{
  _count++;
  TimerTextBlock->Text = "Total Running Time: " + _count.ToString() + " Seconds";
}));


// using CallbackContext::Any
void Playback::DisplayStatus(Platform::String^ text)
{
  _dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, 
                        ref new Windows::UI::Core::DispatchedHandler([=]()
  {
    _OutputStatus->Text += text + "\n";
  }, CallbackContext::Any)); 
}


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.UI.Core
Windows::UI::Core [C++]

Metadata

Windows.winmd

See also

CoreDispatcher

 

 

Show:
© 2014 Microsoft