.NET异步编程总结----四种实现模式代码总结

所属分类: 网络编程 / ASP.NET 阅读数: 252
收藏 0 赞 0 分享

最近很忙,既要外出找工作又要兼顾老板公司的项目。今天在公司,忙里偷闲,总结一下.NET中的异步调用函数的实现方法,DebugLZQ在写这篇博文之前自己先动手写了本文的所有示例代码,开写之前是做过功课的,用代码说话方有说服力。

本文的内容旨在用最简洁的代码来把异步调用的方法说清楚,园子里的高手老鸟可以绕行,不喜勿喷,非诚勿扰~

lz的前一篇文章简单的说了下异步,主要是从理解上来讲;这篇文章主要写具体的实现方法。实现异步编程有4种方法可供选择,这4种访求实际上也对应着4种异步调用的模式,分为“等待”和“回调”两大类。四种方法,我在代码中都进行了详细的注释,这里不罗嗦了,直接用代码说明吧

第一种方法:BeginEnvoke EndEnvoke方法,属于“等待”类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 异步调用实现方法汇总
{
  /// <summary>
  /// 异步调用方法总结:
  /// 1.BeginEnvoke EndEnvoke
  /// 当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕
  /// </summary>
  class Program
  {
    public delegate void PrintDelegate(string s);
    static void Main(string[] args)
    {
      PrintDelegate printDelegate = Print;
      Console.WriteLine("主线程");

      IAsyncResult result= printDelegate.BeginInvoke("Hello World.", null, null);
      Console.WriteLine("主线程继续执行...");
      //当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕
      printDelegate.EndInvoke(result);

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey(true);
    }

    public static void Print(string s)
    {
      Console.WriteLine("异步线程开始执行:"+s);
      Thread.Sleep(5000);
    }
  }
}

需要注意的地方,代码中都有注明了,程序运行结果如下:

第二种方法:WaitOne。同样属于“等待”类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 异步调用实现方法汇总2
{
  /// <summary>
  /// 异步调用方法总结:
  /// 2.WaitOne
  /// 可以看到,与EndInvoke类似,只是用WaitOne函数代码了EndInvoke而已。
  /// </summary>
  class Program
  {
    public delegate void PrintDelegate(string s);
    static void Main(string[] args)
    {
      PrintDelegate printDelegate = Print;
      Console.WriteLine("主线程");
      IAsyncResult result = printDelegate.BeginInvoke("Hello World.", null, null);
      Console.WriteLine("主线程继续执行...");
      result.AsyncWaitHandle.WaitOne(-1, false);

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey(true);
    }
    public static void Print(string s)
    {
      Console.WriteLine("异步线程开始执行:" + s);
      Thread.Sleep(5000);
    }
  }
}

需要注意的地方,代码中都有注明了,程序运行结果如下:

第三种方法:轮询。也是属于“等待”类。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 异步调用实现方法汇总3
{
  /// <summary>
  /// 异步调用方法总结:
  /// 3.轮询
  /// 之前提到的两种方法,只能等下异步方法执行完毕,
  /// 在完毕之前没有任何提示信息,整个程序就像没有响应一样,用户体验不好,
  /// 可以通过检查IasyncResult类型的IsCompleted属性来检查异步调用是否完成,
  /// 如果没有完成,则可以适时地显示一些提示信息
  /// </summary>
  class Program
  {
    public delegate void PrintDelegate(string s);
    static void Main(string[] args)
    {
      PrintDelegate printDelegate = Print;
      Console.WriteLine("主线程:"+Thread.CurrentThread.ManagedThreadId );
      IAsyncResult result = printDelegate.BeginInvoke("Hello world.", null, null);
      Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId + ",继续执行...");
      while (!result.IsCompleted)
      {
        Console.WriteLine(".");
        Thread.Sleep(500);
      }

      Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId + " Press any key to continue...");
      Console.ReadKey(true);
    }
    public static void Print(string s)
    {
      Console.WriteLine("当前线程:" + Thread.CurrentThread.ManagedThreadId + s);
      Thread.Sleep(5000);
    }
  }
}

需要注意的地方,代码中都有注明了,程序运行结果如下:

第四种方法:回调。当然属于“回调”类。推荐!!!!

之前三种方法者在等待异步方法执行完毕后才能拿到执行的结果,期间主线程均处于等待状态。回调和它们最大的区别是,在调用BeginInvoke时只要提供了回调方法,那么主线程就不必要再等待异步线程工作完毕,异步线程在工作结束后会主动调用我们提供的回调方法,并在回调方法中做相应的处理。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 异步调用实现方法汇总4
{
  /// <summary>
  /// 异步调用方法总结:
  /// 4.回调
  /// 之前三种方法者在等待异步方法执行完毕后才能拿到执行的结果,期间主线程均处于等待状态。
  /// 回调和它们最大的区别是,在调用BeginInvoke时只要提供了回调方法,那么主线程就不必要再等待异步线程工作完毕,
  /// 异步线程在工作结束后会主动调用我们提供的回调方法,并在回调方法中做相应的处理,例如显示异步调用的结果。
  /// </summary>
  class Program
  {
    public delegate void PrintDelegate(string s);
    static void Main(string[] args)
    {
      PrintDelegate printDelegate = Print;
      Console.WriteLine("主线程.");
      printDelegate.BeginInvoke("Hello world.", PrintComeplete, printDelegate);
      Console.WriteLine("主线程继续执行...");

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey(true);
    }
    public static void Print(string s)
    { 
      Console.WriteLine("当前线程:"+s);
      Thread.Sleep(5000);
    }
    //回调方法要求
    //1.返回类型为void
    //2.只有一个参数IAsyncResult
    public static void PrintComeplete(IAsyncResult result)
    {
      (result.AsyncState as PrintDelegate).EndInvoke(result);
      Console.WriteLine("当前线程结束." + result.AsyncState.ToString());
    }
  }
}

需要注意的地方,代码中都有注明了,程序运行结果如下:

通过EndInvoke方法得到同步函数的返回值。上面的同步方法返回值为void,我们给个例子:

using System.Diagnostics;
using System.Threading;
using System.Windows;

namespace TestDelegateWrapper
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
      WrapperSyncMethodAsync("ABC");

      Trace.WriteLine("Main thread continue...");
    }

    private delegate string SyncMethod1Delegate(string str);
    
    private void WrapperSyncMethodAsync(string str)
    {
      SyncMethod1Delegate syncMethod1Delegate = SyncMethod1;
      syncMethod1Delegate.BeginInvoke(str, x =>
      {
        var result= syncMethod1Delegate.EndInvoke(x);

        // using the result to do something
        Trace.WriteLine(result);
      }, null);
    }

    private string SyncMethod1(string str)
    {
      Thread.Sleep(2000);
      return str;
    }
  }
}


输出如下:

Main thread continue...
ABC

以上就是四种实现异步调用函数的四种方法,说的很清楚了,就写这么多~希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多