zoukankan      html  css  js  c++  java
  • Windows Phone开发经验谈(16)使用Async CTP简化异步编程

        Windows 8 中已经可以使用C#5.0的“异步”特性,简单优美的代码,大大降低异步编程的复杂性,在Windows Phone7 中也可以利用Async CTP来实现,具体方法如下.

    首先下载Async CTP http://www.microsoft.com/en-us/download/details.aspx?id=9983

    安装的必要条件是Vs2010 Sp1 没安装Sp1的朋友请下载:http://download.microsoft.com/download/E/B/A/EBA0A152-F426-47E6-9E3F-EFB686E3CA20/VS2010SP1dvd1.iso

    Async CTP下载完后不要急于安装..因为该安装程序和众多VS补丁有冲突,即使安装了也无法使用,所以我们必须先排除这些地雷。

    ---------------------------------------------------------------------------------------------------------------

    如果是Windows 8 系统必须在没有安装Silverlight 5之前安装Async CTP 否则无法使用 如果安装了 Silverlight 5请先卸载,如果安装了VS2012 建议也先卸载。

    删除以下补丁可以到控制面板查找:KB2635973, KB2615527, KB2645410

    (具体可以参考:http://blogs.msdn.com/b/lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx

    安装完成之后在我的文档里面就会出现Microsoft Visual Studio Async CTP 这个文件夹 找到AsyncCtpLibrary_Phone.dll 这个文件

    这时候就可以新建一个Windows Phone程序 引用AsyncCtpLibrary_Phone.dll 这个DLL

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Threading.Tasks;
    
    namespace CRSAsyncCTP
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // 构造函数
            public MainPage()
            {
                InitializeComponent();
            }
    
            private int fun(object num)
            {
                int i = (int)num;
                if (i == 0 || i == 1) return 1;
                return i * fun(i - 1);
            }
    
            private async Task<int> FFun(int num)
            {
                return await Task<int>.Factory.StartNew(new Func<object, int>(fun), num);
    
            }
    
            private async void button1_Click(object sender, RoutedEventArgs e)
            {
                string str = textBox1.Text;
                int number = int.Parse(str);
                var result = await FFun(number);
                base.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(result.ToString());
                });
    
            }
    
    
        }
    }

    我做了个DEMO下载地址:CRSAsyncCTP.rar

    好了,就介绍到这里赶快来体验一下吧。

  • 相关阅读:
    LCSTS自动摘要数据集
    知识图谱问答-资料
    多轮对话管理系统Opendial
    关于自动摘要
    使用tatk构建对话代理
    android http协议
    web容器,servlet容器,模型驱动
    <s:property />
    linux下部署java web项目
    @ResultPath("/WEB-INF/jsp/project/")
  • 原文地址:https://www.cnblogs.com/cracker/p/WindowsPhone_AsyncCTP.html
Copyright © 2011-2022 走看看