Transitioning Between Threads

Occasions often arise when one thread needs to schedule work to be executed on another thread. For example, although events on XAML objects are raised on the same UI thread that created the object, this is usually not the case for non-UI objects in the Windows Runtime. Instead, they are raised on whatever background thread happens to be doing the work.

An example of this can be seen with the events defined by MediaCapture, a class described in Chapter 13, “Audio, Video, and Speech.” The following code incorrectly tries to update the UI to notify the user about a failure to capture video from the camera:

Image

Get Universal Windows® Apps with XAML and C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.