Multi-thread Asynchronous Programming: Iasyncaction, Iasyncoperation
Re-imagine Windows 8 Store Apps (45)-multi-line asynchronous programming: Iasyncaction, Iasyncoperation, iasyncactionwithprogress, IASYNCOPERATIONWITHPR Ogress
Introduced
Re-imagine the asynchronous programming of Windows 8 Store Apps
Iasyncaction-no return value, no progress value
Iasyncoperation-There are return values, no progress values
Iasyncactionwithprogress-no return value, there is a progress value
Iasyncoperationwithprogress-There is a return value, there is a progress value
Example
1, demo iasyncaction (no return value, no progress value) of the use
Thread/async/iasyncactiondemo.xaml
<page
x:class= "XamlDemo.Thread.Async.IAsyncActionDemo"
xmlns= "http://schemas.microsoft.com/winfx/ 2006/xaml/presentation "
xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:local=" using: XamlDemo.Thread.Async "
xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "
mc:ignorable=" D ">
<grid background=" Transparent ">
<stackpanel margin=" 0 0 0 ">
<textblock name=" lblmsg "fontsize=" 14.667 "/>
<button name= "btncreateasyncaction" content= "execute a iasyncaction" click= "Btncreateasyncaction_click_1" margin= "0 0 0"/>
<button name= "btncancelasyncaction" content= "Cancel" click= "Btncancelasyncaction_click_1" margin= "0 0 0"/>
</StackPanel>
</Grid>
</Page>
Thread/async/iasyncactiondemo.xaml.cs
* * Demo iasyncaction (no return value, no progress value) usage * * NOTE: * 1, WinRT asynchronous functions are derived from Iasyncinfo * 2, Iasyncaction, Iasyncoperation<tresu Lt>, Iasyncactionwithprogress<tprogress>, Iasyncoperationwithprogress<tresult, TProgress> inherit from Iasyncinfo * * * * Another: * Windows.System.Threading.ThreadPool.RunAsync ()-Return is iasyncaction * * using System.runt Ime.
Interopservices.windowsruntime;
Using System.Threading.Tasks;
Using Windows.foundation;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls; Namespace XamlDemo.Thread.Async {public sealed partial class Iasyncactiondemo:page {private iasyncacti
On _action; Public Iasyncactiondemo () {this.
InitializeComponent (); Private Iasyncaction getasyncaction () {//through System.Runtime.InteropServices.WindowsRun Time.
Asyncinfo Create iasyncaction return Asyncinfo.run (token) =>//CancellationToken token Task.run (() => {token.
WaitHandle.WaitOne (3000); Token.
Throwifcancellationrequested ();
}, token)); } private void Btncreateasyncaction_click_1 (object sender, RoutedEventArgs e) {_action =
Getasyncaction (); Can be await _action//iasyncaction after completion of _action.
Completed = (Asyncinfo, asyncstatus) =>//Iasyncaction asyncinfo, Asyncstatus asyncstatus {//Asyncstatus include: Started, Completed, Canceled, Error lblmsg.text = "Completed, Asy
Ncstatus: "+ asyncstatus.tostring ();
};
Lblmsg.text = "Start execution, 3 seconds to complete";
}//Cancel iasyncaction private void Btncancelasyncaction_click_1 (object sender, RoutedEventArgs e) {if (_Action!= null) _action.
Cancel (); }
}
}