重新想象 Windows 8 Store Apps (45)
[源码下载]重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 异步编程
[*]IAsyncAction - 无返回值,无进度值
[*]IAsyncOperation - 有返回值,无进度值
[*]IAsyncActionWithProgress - 无返回值,有进度值
[*]IAsyncOperationWithProgress - 有返回值,有进度值
示例
1、演示 IAsyncAction(无返回值,无进度值)的用法
Thread/Async/IAsyncActionDemo.xaml
Thread/Async/IAsyncActionDemo.xaml.cs
/*
* 演示 IAsyncAction(无返回值,无进度值)的用法
*
* 注:
* 1、WinRT 中的异步功能均源自 IAsyncInfo
* 2、IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress 均继承自 IAsyncInfo
*
*
* 另:
* Windows.System.Threading.ThreadPool.RunAsync() - 返回的就是 IAsyncAction
*/
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Thread.Async
{
public sealed partial class IAsyncActionDemo : Page
{
private IAsyncAction _action;
public IAsyncActionDemo()
{
this.InitializeComponent();
}
private IAsyncAction GetAsyncAction()
{
// 通过 System.Runtime.InteropServices.WindowsRuntime.AsyncInfo 创建 IAsyncAction
return AsyncInfo.Run(
(token) => // CancellationToken token
Task.Run(
() =>
{
token.WaitHandle.WaitOne(3000);
token.ThrowIfCancellationRequested();
},
token));
}
private void btnCreateAsyncAction_Click_1(object sender, RoutedEventArgs e)
{
_action = GetAsyncAction();
// 可以 await _action
// IAsyncAction 完成后
_action.Completed =
(asyncInfo, asyncStatus) => // IAsyncAction asyncInfo, AsyncStatus asyncStatus
{
// AsyncStatus 包括:Started, Completed, Canceled, Error
lblMsg.Text = "完成了,AsyncStatus: " + asyncStatus.ToString();
};
lblMsg.Text = "开始执行,3 秒后完成";
}
// 取消 IAsyncAction
private void btnCancelAsyncAction_Click_1(object sender, RoutedEventArgs e)
{
if (_action != null)
_action.Cancel();
}
}
}
2、演示 IAsyncOperation(有返回值,无进度值)的用法
Thread/Async/IAsyncOperationDemo.xaml
Thread/Async/IAsyncOperationDemo.xaml.cs
/*
* 演示 IAsyncOperation(有返回值,无进度值)的用法
*
* 注:
* 1、WinRT 中的异步功能均源自 IAsyncInfo
* 2、IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress 均继承自 IAsyncInfo
*/
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Thread.Async
{
public sealed partial class IAsyncOperationDemo : Page
{
private IAsyncOperation _operation;
public IAsyncOperationDemo()
{
this.InitializeComponent();
}
private IAsyncOperation GetAsyncOperation(int x, int y)
{
// 通过 System.Runtime.InteropServices.WindowsRuntime.AsyncInfo 创建 IAsyncOperation
return AsyncInfo.Run(
(token) => // CancellationToken token
Task.Run(
() =>
{
token.WaitHandle.WaitOne(3000);
token.ThrowIfCancellationRequested();
// 返回结果
return x * y;
},
token));
}
private void btnCreateAsyncOperation_Click_1(object sender, RoutedEventArgs e)
{
_operation = GetAsyncOperation(10, 10);
// 可以 await _operation
// IAsyncOperation 完成后
_operation.Completed =
(asyncInfo, asyncStatus) => // IAsyncAction asyncInfo, AsyncStatus asyncStatus
{
// AsyncStatus 包括:Started, Completed, Canceled, Error
lblMsg.Text = "完成了,AsyncStatus: " + asyncStatus.ToString();
if (asyncStatus == AsyncStatus.Completed)
{
lblMsg.Text += Environment.NewLine;
// 获取异步操作的返回结果
lblMsg.Text += "结果: " + asyncInfo.GetResults().ToString();
}
};
lblMsg.Text = "开始执行,3 秒后完成";
}
// 取消 IAsyncOperation
private void btnCancelAsyncOperation_Click_1(object sender, RoutedEventArgs e)
{
if (_operation != null)
_operation.Cancel();
}
}
}
3、演示 IAsyncActionWithProgress(无返回值,有进度值)的用法
Thread/Async/IAsyncActionWithProgressDemo.xaml
Thread/Async/IAsyncActionWithProgressDemo.xaml.cs
/*
* 演示 IAsyncActionWithProgress(无返回值,有进度值)的用法
*
* 注:
* 1、WinRT 中的异步功能均源自 IAsyncInfo
* 2、IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress 均继承自 IAsyncInfo
*/
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Thread.Async
{
public sealed partial class IAsyncActionWithProgressDemo : Page
{
private IAsyncActionWithProgress _action;
public IAsyncActionWithProgressDemo()
{
this.InitializeComponent();
}
private IAsyncActionWithProgress GetAsyncActionWithProgress()
{
// 通过 System.Runtime.InteropServices.WindowsRuntime.AsyncInfo 创建 IAsyncActionWithProgress
return AsyncInfo.Run(
(token, progress) => // CancellationToken token, IProgress progress
Task.Run(
() =>
{
// 报告进度(进度是一个 int 值)
progress.Report(0);
int percent = 0;
while (percent < 100)
{
token.WaitHandle.WaitOne(100);
token.ThrowIfCancellationRequested();
percent++;
// 报告进度(进度是一个 int 值)
progress.Report(percent);
}
},
token));
}
private void btnCreateAsyncActionWithProgress_Click_1(object sender, RoutedEventArgs e)
{
_action = GetAsyncActionWithProgress();
// 可以 await _action
// IAsyncActionWithProgress 完成后
_action.Completed =
(asyncInfo, asyncStatus) => // IAsyncAction asyncInfo, AsyncStatus asyncStatus
{
// AsyncStatus 包括:Started, Completed, Canceled, Error
lblMsg.Text = "完成了,AsyncStatus: " + asyncStatus.ToString();
};
// IAsyncActionWithProgress 接收到进度后
_action.Progress =
(asyncInfo, progressInfo) => // IAsyncActionWithProgress asyncInfo, TProgress progressInfo
{
// 进度是一个 int 值
lblProgress.Text = "进度: " + progressInfo.ToString();
};
lblMsg.Text = "开始执行";
}
// 取消 IAsyncActionWithProgress
private void btnCancelAsyncActionWithProgress_Click_1(object sender, RoutedEventArgs e)
{
if (_action != null)
_action.Cancel();
}
}
}
4、演示 IAsyncOperationWithProgress(有返回值,有进度值)的用法
Thread/Async/IAsyncOperationWithProgressDemo.xaml
Thread/Async/IAsyncOperationWithProgressDemo.xaml.cs
/*
* 演示 IAsyncOperationWithProgress(有返回值,有进度值)的用法
*
* 注:
* 1、WinRT 中的异步功能均源自 IAsyncInfo
* 2、IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress 均继承自 IAsyncInfo
*
*
* 另:
* Windows.Web.Syndication.SyndicationClient.RetrieveFeedAsync() - 返回的就是 IAsyncOperationWithProgress
*/
using System;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Thread.Async
{
public sealed partial class IAsyncOperationWithProgressDemo : Page
{
private IAsyncOperationWithProgress _operation;
public IAsyncOperationWithProgressDemo()
{
this.InitializeComponent();
}
private IAsyncOperationWithProgress GetAsyncOperationWithProgress()
{
// 通过 System.Runtime.InteropServices.WindowsRuntime.AsyncInfo 创建 IAsyncOperationWithProgress
return AsyncInfo.Run(
(token, progress) =>
Task.Run(
() =>
{
// 报告进度(进度是一个 int 值)
progress.Report(0);
int percent = 0;
while (percent < 100)
{
token.WaitHandle.WaitOne(100);
token.ThrowIfCancellationRequested();
percent++;
// 报告进度(进度是一个 int 值)
progress.Report(percent);
}
// 返回结果
return "成功了";
},
token));
}
private void btnCreateAsyncOperationWithProgress_Click_1(object sender, RoutedEventArgs e)
{
_operation = GetAsyncOperationWithProgress();
// 可以 await _operation
// IAsyncOperationWithProgress 完成后
_operation.Completed =
(asyncInfo, asyncStatus) => // IAsyncAction asyncInfo, AsyncStatus asyncStatus
{
// AsyncStatus 包括:Started, Completed, Canceled, Error
lblMsg.Text = "完成了,AsyncStatus: " + asyncStatus.ToString();
if (asyncStatus == AsyncStatus.Completed)
{
lblMsg.Text += Environment.NewLine;
// 获取异步操作的返回结果
lblMsg.Text += "结果: " + asyncInfo.GetResults().ToString();
}
};
// IAsyncOperationWithProgress 接收到进度后
_operation.Progress =
(asyncInfo, progressInfo) => // IAsyncActionWithProgress asyncInfo, TProgress progressInfo
{
// 进度是一个 int 值
lblProgress.Text = "进度: " + progressInfo.ToString();
};
lblMsg.Text = "开始执行";
}
// 取消 IAsyncOperationWithProgress
private void btnCancelAsyncOperationWithProgress_Click_1(object sender, RoutedEventArgs e)
{
if (_operation != null)
_operation.Cancel();
}
}
}
OK
[源码下载]
页:
[1]