• 第十三章 异步编程


    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
  • 相关阅读:
    根据文件名或文件扩展名获取文件的默认图标
    TreeView实现类似Outlook在收件箱后面显示新邮件数
    取每组数据的第一条记录的SQL语句
    Stream 和 byte[] 之间的转换
    使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
    C# 创建临时文件
    Tomcat 服务不能启动的问题
    VS2008 椭圆曲线签名(ECDSA)
    2007年12月23日在博客园的排名进入了前300名
    现代软件工程 作业 3 团队作业
  • 原文地址:https://www.cnblogs.com/liuslayer/p/7146060.html
Copyright © 2020-2023  润新知