AsyncOperationManager Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Provides concurrency management for classes that support asynchronous method calls. This class cannot be inherited.
Assembly: System (in System.dll)
The AsyncOperationManager type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | SynchronizationContext | Gets or sets the SynchronizationContext for the asynchronous operation. |
| Name | Description | |
|---|---|---|
![]() ![]() | CreateOperation | Returns an AsyncOperation for tracking the duration of a particular asynchronous operation. |
If your class must provide asynchronous behavior according to the Event-based Asynchronous Pattern Overview topic in the .NET Framework documentation, your scenario may also have concurrency management requirements. Among these is the requirement to make sure that event handlers are called on a thread or context that is appropriate for the application model. The AsyncOperationManager provides a convenient way to create a class that runs correctly under all application models supported by the .NET Framework.
The AsyncOperationManager class has one method, CreateOperation, which returns an System.ComponentModel::AsyncOperation object that can be used to track the duration of a particular asynchronous task. The System.ComponentModel::AsyncOperation for a task can be used to alert clients when a task completes. It can also be used to post progress updates and incremental results without terminating the operation.
For more information about how to implement asynchronous classes, see Implementing the Event-based Asynchronous Pattern in the .NET Framework documentation.
Note: |
|---|
The BackgroundWorker class provides a simpler programming model for handling asynchronous behavior. Implementing the event-based asynchronous pattern by using AsyncOperationManager is more complex, but also more flexible, and is necessary if you must be able to communicate between the parent class and the worker class by using custom events. By using BackgroundWorker, you are limited to its ProgressChanged and RunWorkerCompleted events. Depending on the scenario, the AsyncOperationManager pattern may have less overhead than using BackgroundWorker because it is delegate-based and not event-based. |
The following code example demonstrates how to use the AsyncOperationManager class to create a class that supports asynchronous operations for any application model. It simulates a long-running data-gathering operation by using Thread.Sleep statements. Progress reports, incremental results, cancellation, and completion notifications are handled by the AsyncOperation class, which makes sure that the client's event handlers are called on the correct thread or context. Asynchronous processing is started when the Load button is clicked. When the operation is complete, the data is shown in a ListBox control. After it starts, the operation's progress is shown in a box below the buttons, the Load button is disabled, and the Cancel button is enabled.
The MainPage class represents the user interface and performs the following functions:
Calls the ReturnRows method of the PersonDataSource class asynchronously.
Disables the Load button and enables the Cancel button when the process starts.
Updates the progress reporting text boxes during the load process.
Provides a status message when the operation ends.
The Person class shows the format of the data that is returned by the PersonDataSource class and displayed by the ListBox. The PersonDataSource class supports asynchronous processing for the simulated long-running data-gathering operation.
<phone:PhoneApplicationPage x:Class="AsyncOperationManagerExample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="800" d:DesignWidth="480"> <StackPanel x:Name="LayoutRoot" Background="Transparent"> <ListBox x:Name="dg" Height="250"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <ListBoxItem Content="{Binding Name}" Margin="5"/> <ListBoxItem Content="{Binding Age}" Margin="5"/> <ListBoxItem Content="{Binding Birthday}" Margin="5"/> <ListBoxItem Content="{Binding Available}" Margin="5"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Orientation="Horizontal"> <Button x:Name="loadButton" Content="Load" Width="190" Margin="5" Click="loadButton_Click"/> <Button x:Name="cancelButton" Content="Cancel" Width="190" Margin="5" Click="cancelButton_Click" IsEnabled="False"/> </StackPanel> <Border BorderBrush="Black" BorderThickness="1" Padding="2" Margin="5"> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <TextBlock Width="170" TextAlignment="Right" Text="Percent Complete: "/> <TextBlock x:Name="percentCompleteTextBlock" FontWeight="Bold"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Width="170" TextAlignment="Right" Text="Latest Name: " /> <TextBlock x:Name="latestNameTextBlock" FontWeight="Bold"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Width="170" TextAlignment="Right" Text="Status: " /> <TextBlock x:Name="statusMessageBlock" FontWeight="Bold"/> </StackPanel> </StackPanel> </Border> </StackPanel> </phone:PhoneApplicationPage>



Note: