温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.NET中怎么实现异步编程

发布时间:2021-08-10 15:12:44 来源:亿速云 阅读:134 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关.NET中怎么实现异步编程,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

第一种方法: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);
    }
  }
}

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

.NET中怎么实现异步编程

第二种方法: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);
    }
  }
}

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

.NET中怎么实现异步编程

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

 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);
    }
  }
}

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

.NET中怎么实现异步编程

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

之前三种方法者在等待异步方法执行完毕后才能拿到执行的结果,期间主线程均处于等待状态。回调和它们最大的区别是,在调用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());
    }
  }
}

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

.NET中怎么实现异步编程

通过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;
    }
  }
}

上述就是小编为大家分享的.NET中怎么实现异步编程了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI