C#中异步回调函数用法实例
作者:dingding 时间:2023-01-05 13:10:53
本文实例讲述了C#中异步回调函数用法。分享给大家供大家参考。具体如下:
static void Main(string[] args)
{
Func<string,string> showMessage = ShowMessage;
//设置了回调函数Completed,不能有返回值
IAsyncResult result = showMessage.BeginInvoke("测试异步委托",new AsyncCallback(Completed),null);
//半段异步是否结束
while(!result.IsCompleted)
{
Console.WriteLine("主线程可以进行其它的操作!");
}
Console.ReadLine();
}
static string ShowMessage(string x)
{
string current = string.Format("当前线程id为{0}",Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(3000);
return string.Format("{0},输入为{1}", current, x);
}
static void Completed(IAsyncResult result)
{
Console.WriteLine("异步完成!");
//获取委托对象,并用EndInvoke方法获取返回结果
AsyncResult _result = (AsyncResult) result;
Func<string, string> showMessage = (Func<string, string>) _result.AsyncDelegate;
//结束异步操作并输出
Console.WriteLine(showMessage.EndInvoke(_result));
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,异步,回调函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)
2022-02-27 06:53:11
浅谈java安全编码指南之死锁dead lock
2023-06-22 04:49:19
详解基于spring多数据源动态调用及其事务处理
2023-06-23 14:37:25
![](https://img.aspxhome.com/file/2023/0/57530_0s.png)
SpringBoot中如何对actuator进行关闭
2022-11-30 01:56:37
![](https://img.aspxhome.com/file/2023/9/62349_0s.png)
arthas排查jvm中CPU占用过高问题解决
2022-07-15 20:31:49
![](https://img.aspxhome.com/file/2023/7/86847_0s.png)
Jetpack Compose按钮组件使用实例详细讲解
2022-09-02 23:16:57
![](https://img.aspxhome.com/file/2023/9/139029_0s.png)
java多线程下载实例详解
2023-05-19 05:53:37
![](https://img.aspxhome.com/file/2023/9/116179_0s.png)
C#在MEF框架中手动导入依赖模块
2023-05-24 19:34:43
Android 5.0+ 屏幕录制实现的示例代码
2023-11-19 13:02:30
![](https://img.aspxhome.com/file/2023/7/116007_0s.png)
详解C#对XML、JSON等格式的解析
2022-06-04 22:45:50
![](https://img.aspxhome.com/file/2023/5/68615_0s.jpg)
详解C#对路径...的访问被拒绝解决过程
2021-06-01 11:12:10
Android中检查、监听电量和充电状态的方法
2023-05-15 23:23:19
详解Spring Boot 部署jar和war的区别
2022-10-25 18:48:29
java继承中的构造方法实例解析
2022-07-26 02:56:00
C#值类型、引用类型中的Equals和==的区别浅析
2022-09-05 23:04:24
![](https://img.aspxhome.com/file/2023/3/98283_0s.png)
Android自定义UI实现微信语音
2022-04-18 10:47:41
Android Hilt Retrofit Paging3使用实例
2022-11-20 21:06:59
![](https://img.aspxhome.com/file/2023/5/102615_0s.gif)
Android点击事件之多点触摸与手势识别的实现
2022-10-12 05:12:28
![](https://img.aspxhome.com/file/2023/1/137801_0s.jpg)
Android微信Tinker热更新详细使用
2021-08-24 01:58:47
![](https://img.aspxhome.com/file/2023/9/139349_0s.gif)
C#泛型委托的用法实例分析
2021-09-16 23:29:52
![](https://img.aspxhome.com/file/2023/9/92909_0s.jpg)