zoukankan      html  css  js  c++  java
  • 第十三章 异步编程

    Foundations

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Threading;
    
    namespace Foundations
    {
      class Program
      {
        static void Main()
        {
          var ctx = new DispatcherSynchronizationContext();
    
          // SynchronizationContext.SetSynchronizationContext(ctx);
          // CallerWithAsync();
          // CallerWithContinuationTask();
          // CallerWithAwaiter();
          // MultipleAsyncMethods();
          // MultipleAsyncMethodsWithCombinators1();
          //   MultipleAsyncMethodsWithCombinators2();
          ConvertingAsyncPattern();
          Console.ReadLine();
    
    
        }
    
        private static async void ConvertingAsyncPattern()
        {
          string r = await Task<string>.Factory.FromAsync<string>(BeginGreeting, EndGreeting, "Angela", null);
          Console.WriteLine(r);
        }
    
    
        private async static void MultipleAsyncMethods()
        {
          string s1 = await GreetingAsync("Stephanie");
          string s2 = await GreetingAsync("Matthias");
          Console.WriteLine("Finished both methods.
     Result 1: {0}
     Result 2: {1}", s1, s2);
        }
    
        private async static void MultipleAsyncMethodsWithCombinators1()
        {
          Task<string> t1 = GreetingAsync("Stephanie");
          Task<string> t2 = GreetingAsync("Matthias");
          await Task.WhenAll(t1, t2);
          Console.WriteLine("Finished both methods.
     Result 1: {0}
     Result 2: {1}", t1.Result, t2.Result);
        }
    
        private async static void MultipleAsyncMethodsWithCombinators2()
        {
          Task<string> t1 = GreetingAsync("Stephanie");
          Task<string> t2 = GreetingAsync("Matthias");
          string[] result = await Task.WhenAll(t1, t2);
          Console.WriteLine("Finished both methods.
     Result 1: {0}
     Result 2: {1}", result[0], result[1]);
        }
    
        private static void CallerWithContinuationTask()
        {
          Console.WriteLine("started CallerWithContinuationTask in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
    
          var t1 = GreetingAsync("Stephanie");
    
    
          t1.ContinueWith(t =>
            {
              string result = t.Result;
              Console.WriteLine(result);
              Console.WriteLine("finished CallerWithContinuationTask in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
            });
    
        }
    
        private static void CallerWithAwaiter()
        {
          Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
          string result = GreetingAsync("Matthias").GetAwaiter().GetResult();
          Console.WriteLine(result);
          Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
        }
    
        private async static void CallerWithAsync()
        {
          Console.WriteLine("started CallerWithAsync in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
          string result = await GreetingAsync("Stephanie");
          Console.WriteLine(result);
          Console.WriteLine("finished GreetingAsync in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
        }
    
        private async static void CallerWithAsync2()
        {
          Console.WriteLine("started CallerWithAsync in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
          Console.WriteLine(await GreetingAsync("Stephanie"));
          Console.WriteLine("finished GreetingAsync in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
        }
    
        static Task<string> GreetingAsync(string name)
        {
          return Task.Run<string>(() =>
            {
              Console.WriteLine("running greetingasync in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
              return Greeting(name);
            });
        }
    
        static string Greeting(string name)
        {
          Console.WriteLine("running greeting in thread {0} and task {1}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
    
          Thread.Sleep(3000);
          return string.Format("Hello, {0}", name);
        }
    
        private static Func<string, string> greetingInvoker = Greeting;
    
        static IAsyncResult BeginGreeting(string name, AsyncCallback callback, object state)
        {
          return greetingInvoker.BeginInvoke(name, callback, state);
        }
    
        static string EndGreeting(IAsyncResult ar)
        {
          return greetingInvoker.EndInvoke(ar);
        }
    
      }
    }
    View Code
  • 相关阅读:
    CentOS8安装Mysql5.7
    CentOS8搭建FTP服务器
    CentOS8安装jdk1.8
    基于可穿戴设备的医疗监护系统
    air530GPS数据通过air202上传阿里云
    bzoj2594: [Wc2006]水管局长数据加强版
    bzoj3091: 城市旅行
    Problem A. Array Factory XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016
    hdu5716
    bzoj2002: [Hnoi2010]Bounce 弹飞绵羊
  • 原文地址:https://www.cnblogs.com/liuslayer/p/7146060.html
Copyright © 2011-2022 走看看