Winphone开发之异步任务处理

这一篇只能算是备注,异步任务这一块自己还要多复习操作系统。

下面是XAML:

<phone:PhoneApplicationPage
    x:Class="AsyncTask.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"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="480">
        <Button Content="异步" HorizontalAlignment="Left" Margin="178,522,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.348,0.043" Click="Button_Click"/>
        <Button Content="UI" HorizontalAlignment="Left" Margin="199,430,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        <TextBlock x:Name="Tb" HorizontalAlignment="Left" Margin="165,298,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="66" Width="155"/>
        <Button Content="同步" HorizontalAlignment="Left" Margin="178,617,0,0" VerticalAlignment="Top" Click="Button_Click_2"/>
    </Grid>


</phone:PhoneApplicationPage>

下面是CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using AsyncTask.Resources;
using System.Threading.Tasks;
using System.Threading;

namespace AsyncTask
{
    public partial class MainPage : PhoneApplicationPage
    {
        private int num = 0;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

        }

        private async void  Button_Click(object sender, RoutedEventArgs e)
        {
            await A();
        }

        private async Task A()
        {
            await Task.Delay(10000);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Tb.Text = num.ToString();
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Thread.Sleep(10000);
        }

    }
}

下面是程序截图:


当按下异步按钮的时候再按UI按钮TEXTBLOCK会改变,当按下同步按钮按UI按钮要等10S的时间才有相应。

注意:

异步方法类型只有void ,Task或Task<TResult>

async声明该方法是异步,当时实际上此时并没有起异步,await才是真正进行异步处理。

await something,只有当something执行完毕之后await之后的代码才会执行。

await关键字必须要在async关键字内


实例二:

这个实例告诉了我们Lambda表达式很重要。

<phone:PhoneApplicationPage
    x:Class="PhoneApp6.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"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="480">
        <TextBox Loaded="txt_Loaded" Name="txt" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="456" Margin="14,303,0,0"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="171,455,0,0" VerticalAlignment="Top"/>
    </Grid>

</phone:PhoneApplicationPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp6.Resources;
using System.Threading.Tasks;
using System.Threading;

namespace PhoneApp6
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        private async void txt_Loaded(object sender, RoutedEventArgs e)
        {
            while (true)
            {
                String t = await getTime();
                txt.Text = t;
            }
        }


        private async Task<String> getTime()
        {
            return await Task<String>.Run<String>(() =>
                {
                    Thread.Sleep(1000);
                    return DateTime.Now.Hour + "--" + DateTime.Now.Minute + "--" + DateTime.Now.Second + "--" + DateTime.Now.Millisecond;
                });
        }
    }
}

一秒之后TextBox就会刷新显示当前的时间,可以通过点击Button知道UI线程是没有被阻塞的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值