Winphone開發之非同步任務處理
阿新 • • 發佈:2019-02-17
這一篇只能算是備註,非同步任務這一塊自己還要多複習作業系統。
下面是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執行緒是沒有被阻塞的。