网站首页 > 技术文章 正文
第一种方法: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 异步调用实现方法汇总
{
/// <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);
}
}
}
第三种方法:轮询。也是属于“等待”类。
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
以上就是四种实现异步调用函数的四种方法,说的很清楚了,就写这么多~
reference:.NET Framework Delegates: Understanding Asynchronous Delegates
DebugLZQ后续之作:从C#5.0说起:再次总结C#异步调用方法发展史
大家都不是牛人,多多学习,多多交流【请点击下面的“绿色通道”---“关注DebugLZQ”,共同交流进步~】
分类: Design Patterns, Multithreading Asynchronous, .NET Miscellaneous
好文要顶 关注我 收藏该文
猜你喜欢
- 2024-10-25 优秀后端都应该具备哪些开发好习惯
- 2024-10-25 分享50个让你代码更好的小建议(好用的代码)
- 2024-10-25 Spring AOP里的静态代理和动态代理,你真的了解吗?
- 2024-10-25 代码保护软件 VMProtect 用户手册之准备项目: 使用标记
- 2024-10-25 写代码有这些想法,同事才不会认为你是复制粘贴程序员
- 2024-10-25 用Java创建对象的5种不同方法(java创建对象的几种方式)
- 2024-10-25 DispatcherObject(dispatchertimer)
- 2024-10-25 WPF效果第二百一十篇之NPOI插入图片
- 2024-10-25 【译】ConfigureAwait FAQ(configgenerator翻译)
- 2024-10-25 C# 实现 Linux 视频会议(源码,支持信创环境,银河麒麟,统信UOS)
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 514℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 488℃MySQL service启动脚本浅析(r12笔记第59天)
- 467℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 465℃启用MySQL查询缓存(mysql8.0查询缓存)
- 445℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 425℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 422℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)