Async

From no name for this wiki
Jump to: navigation, search

async und await

Task.Run()

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Button_Click: " + Thread.CurrentThread.GetHashCode());
            string p = await Task.Run(() => { return GetString(); });
            myLabel.Content = p;
        }
 
        static string GetString()
        {
            //GetString wird nicht im UI Thread ausgeführt.
            Console.WriteLine("GetStringStart: " + Thread.CurrentThread.GetHashCode());
            System.Threading.Thread.Sleep(10000);
            Console.WriteLine("GetStringEnd: " + Thread.CurrentThread.GetHashCode());
            return "Hello World asynchronous";
 
        }
 
    }
}

Converting old style System.ComponentModel.AsyncCompletedEventArgs to new style

internal static class Extension
{
        private static void TransferCompletion<T>(
            TaskCompletionSource<T> tcs, System.ComponentModel.AsyncCompletedEventArgs e, 
    Func<T> getResult)
        {
            if (e.Error != null)
            {
                tcs.TrySetException(e.Error);
            }
            else if (e.Cancelled)
            {
                tcs.TrySetCanceled();
            }
            else
            {
                tcs.TrySetResult(getResult());
            }
        }
 
        public static Task<loginCompletedEventArgs> LoginAsyncTask(this YChatWebService.WebServiceControllerPortTypeClient client, string userName, string password)
        {
            var tcs = new TaskCompletionSource<loginCompletedEventArgs>();
            client.loginCompleted += (s, e) => TransferCompletion(tcs, e, () => e);
            client.loginAsync(userName, password);
            return tcs.Task;
        }
 }

Avoid thread switching

async Task SomeMethod()
{
    for (int i = 0; i < 1000000; i++)
    {
        await SomeOtherMethod().ConfigureAwait(false);
    }
}
 
async Task SomeOtherMethod()
{
    await ...
}

Parallelism

Serielle Abarbeitung:

private async void button_Click(object sender, EventArgs e)
{
    this.pictureBox1.Image = await GetImage("http://imageurl1");
    this.pictureBox2.Image = await GetImage("http://imageurl2");
    this.pictureBox3.Image = await GetImage("http://imageurl3");
}

Parallel 1:

private async void button_Click(object sender, EventArgs e)
{
    Task<Image> task1 = GetImage("http://imageurl1"); //Download startet beim Aufruf GetImage
    Task<Image> task2 = GetImage("http://imageurl2");
    Task<Image> task3 = GetImage("http://imageurl3");
 
    this.pictureBox1.Image = await task1;
    this.pictureBox2.Image = await task2;
    this.pictureBox3.Image = await task3;
}

Parallel 2:

private async void button_Click(object sender, EventArgs e)
{
    Task<Image> task1 = GetImage("http://imageurl1");
    Task<Image> task2 = GetImage("http://imageurl2");
    Task<Image> task3 = GetImage("http://imageurl3");
 
    await Task.WhenAll(task1, task2, task3);
 
    this.pictureBox1.Image = task1.Result;
    this.pictureBox2.Image = task2.Result;
    this.pictureBox3.Image = task3.Result;
}

yield

public class PowersOf2
{
    static void Main()
    {
        // Display powers of 2 up to the exponent of 8: 
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }
 
    public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent)
    {
        int result = 1;
 
        for (int i = 0; i < exponent; i++)
        {
            result = result * number;
            yield return result;
        }
    }
 
    // Output: 2 4 8 16 32 64 128 256
}