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
- Winphone开发之异步任务处理
- Winphone开发之BackgroupWorker
- Winphone开发之IsolatedStorageFile
- WinPhone开发之GeoCoordinateWatcher
- Winphone开发之JSON解析
- Winphone开发之保存设置
- Winphone开发之页面导航
- Winphone开发之资源字典
- 学习WinPhone开发之路
- winphone--多任务之墓碑机制
- Android开发AsyncTask异步处理任务
- Android开发AsyncTask异步处理任务
- Winphone开发之下载+显示图片
- Winphone开发之数据绑定(1)
- Winphone开发之数据绑定(2)
- Winphone开发之数据绑定(3)
- Winphone开发之数据绑定(4)
- Winphone开发之动态加载XAML
- SQL Server 2008 各种DateTime的取值范围
- Solaris字符集的设置和FTP的使用
- mysql性能优化-慢查询分析、优化索引和配置
- 类之间的关系
- windows常用命令总结
- Winphone开发之异步任务处理
- 桶排序
- 设计模式六大原则(2):里氏替换原则(Liskov Substitution Principle)
- 折半查找
- 【翻译】Android Support Library Features(二)
- 固定大小内存池设计与实现
- 设计模式六大原则(3):依赖倒置原则(Dependence Inversion Principle)
- 建立类图步骤与方法
- 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)