Difference between revisions of "Async"
(Created page with "= async und await =") |
(→async und await) |
||
Line 1: | Line 1: | ||
= async und await = | = async und await = | ||
+ | == Task.Run() == | ||
+ | <source lang="csharp"> | ||
+ | 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() | ||
+ | { | ||
+ | Console.WriteLine("GetStringStart: " + Thread.CurrentThread.GetHashCode()); | ||
+ | System.Threading.Thread.Sleep(10000); | ||
+ | Console.WriteLine("GetStringEnd: " + Thread.CurrentThread.GetHashCode()); | ||
+ | return "Hello World asynchronous"; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </source> |
Revision as of 07:18, 16 October 2014
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()
{
Console.WriteLine("GetStringStart: " + Thread.CurrentThread.GetHashCode());
System.Threading.Thread.Sleep(10000);
Console.WriteLine("GetStringEnd: " + Thread.CurrentThread.GetHashCode());
return "Hello World asynchronous";
}
}
}