设为首页 收藏本站
查看: 1131|回复: 0

重新想象 Windows 8 Store Apps (45)

[复制链接]
发表于 2015-5-22 08:14:43 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69342-1-1.html 上篇帖子: 重新想象 Windows 8 Store Apps (46) 下篇帖子: Windows phone 8 学习笔记(6) 多任务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表