Windows 8 Store Apps Learning (44) Multi-thread asynchronous programming

Source: Internet
Author: User
Tags thread xmlns

Multi-thread Asynchronous programming: Classic and newest asynchronous programming model, Iasyncinfo and Task conversion

Introduced

Re-imagine the asynchronous programming of Windows 8 Store Apps

Classic Asynchronous Programming model (IAsyncResult)

The latest asynchronous programming model (async and await)

Convert Iasyncinfo into a Task

Convert Task to Iasyncinfo

Example

1, using the classic asynchronous programming model (IAsyncResult) to implement a class to support asynchronous operations

Thread/async/classicasync.cs

* * Using the Classic Asynchronous programming model (IAsyncResult) to implement a class/using System that supports asynchronous operations;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
    
Using System.Threading.Tasks;  Namespace XamlDemo.Thread.Async {public class Classicasync {private delegate string hellodelegate (string
    
        name);
    
        Private Hellodelegate _hellodelegate;
        Public Classicasync () {_hellodelegate = new hellodelegate (Hello); private string Hello (string name) {new ManualResetEvent (false).
            WaitOne (3000);
        Return "Hello:" + name;
        //Begin method Public IAsyncResult Beginrun (string name, AsyncCallback callback, Object State) {//new thread, to execute the Hello () method, callback is a callback, state is the context return _hellodelegate.begininvoke (name, callback
        , state);
          The//End method public string Endrun (IAsyncResult ar) {  if (AR = null) throw new NullReferenceException ("IAsyncResult cannot be null");
        return _hellodelegate.endinvoke (AR); }
    }
}

Thread/async/classicasyncdemo.xaml

<page
    x:class= "XamlDemo.Thread.Async.ClassicAsyncDemo"
    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= "Btniasyncresult" content= "IAsyncResult Demo" click= "btniasyncresult_click_1" margin= "0 10 0 0" />
    
        </StackPanel>
    </Grid>
</Page>

Thread/async/classicasyncdemo.xaml.cs

* * Demonstrates how to perform asynchronous operations through the classic asynchronous programming model (IASYNCRESULT) * IAsyncResult-Asynchronous Operation result * asyncstate-context * iscompleted-Asynchronous operation Completed * AsyncWaitHandle-Gets the System.Threading.WaitHandle object that waits for the asynchronous operation to complete (via WaitHandle.WaitOne () on the current thread) * * usin
G System;
Using Windows.UI.Xaml;
    
Using Windows.UI.Xaml.Controls; Namespace XamlDemo.Thread.Async {public sealed partial class Classicasyncdemo:page {SYSTEM.THREADING.S
    
        Ynchronizationcontext _synccontext; Public Classicasyncdemo () {this.
    
            InitializeComponent ();
        Gets the current UI thread _synccontext = System.Threading.SynchronizationContext.Current; } private void Btniasyncresult_click_1 (object sender, RoutedEventArgs e) {Classicasync C
    
            Lassicasync = new Classicasync ();
    
            IAsyncResult ar = Classicasync.beginrun ("Webabcd", New AsyncCallback (Callback), classicasync);
        Lblmsg.text = "Start execution, 3 seconds to complete";
}    
        private void Callback (IAsyncResult ar) {classicasync Classicasync = (classicasync) ar.
            asyncstate;
    
            string result = Classicasync.endrun (AR);
                _synccontext.post ((CTX) => {lblmsg.text = result;
        }, NULL); }
    }
}

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.