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
  • 相关阅读:
    shell脚本通过ping命令来获取平均延时
    源码大招:不服来战!撸这些完整项目,你不牛逼都难!
    最新最全的 Android 开源项目合集
    3.环境搭建-Hadoop(CDH)集群搭建
    mycat安装和测试
    LVS+keepalived
    Linux上利用NFS实现远程挂载
    CentOS下MySQL主从同步配置
    Nginx配置文件(nginx.conf)配置详解
    Storm集群搭建
  • 原文地址:https://www.cnblogs.com/liuslayer/p/7146060.html
Copyright © 2011-2022 走看看