Winphone开发之异步任务处理

来源:互联网 发布:gta5美女捏脸详细数据 编辑:程序博客网 时间:2024/05/16 16:57

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

下面是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
原创粉丝点击