Windows 8 Store Apps Learning (45) Multi-thread asynchronous programming: Iasyncaction

Source: Internet
Author: User
Tags thread xmlns

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&gt, Iasyncactionwithprogress<tprogress&gt, 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 (); }
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.