C#中异步使用及回调
2024-10-18 17:21:56
1. 一句话理解异步
我叫你去吃饭,叫完你不去,那我就会一直等你,直到你和我一起去吃饭。这叫同步!
我叫你去吃饭,叫完不管你去不去,我都不会等你,我自己去吃饭。这叫异步!
2. 异步使用
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
}; action.BeginInvoke("异步参数",null,null); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
3. 异步回调
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
action.BeginInvoke("异步参数", callback, null); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
4. 异步回调带参数
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
action.BeginInvoke("异步参数", callback, "无名小虾"); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
5. 异步等待
第一种方式:IsCompleted
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾"); while (!asyncResult.IsCompleted)
{
Console.WriteLine("等待中......");
Thread.Sleep(200);
} Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
第二种方式:WaitOne()
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾"); asyncResult.AsyncWaitHandle.WaitOne(); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
第三种方式:EndInvoke()
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Action<string> action = t =>
{
for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0}", Thread.CurrentThread.ManagedThreadId,t);
};
AsyncCallback callback = t =>
{
Console.WriteLine(t.AsyncState);
Console.WriteLine("这里是回调函数 当前线程是{0}", Thread.CurrentThread.ManagedThreadId);
};
IAsyncResult asyncResult = action.BeginInvoke("异步参数", callback, "无名小虾"); action.EndInvoke(asyncResult); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
6. 异步返回值
主线程获取到返回值
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Func<string,string> func = t =>
{
string result = "这是我的返回值"; for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
return result;
}; IAsyncResult asyncResult = func.BeginInvoke("异步参数", null, null); string res = func.EndInvoke(asyncResult); Console.WriteLine("获取到返回值:{0}", res); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
回调获取到返回值
static void Main(string[] args)
{
Console.WriteLine("----------主程序开始,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Func<string,string> func = t =>
{
string result = "这是我的返回值"; for (int k = 0; k < 1000000000; k++)
{ }
Console.WriteLine("当前参数是{1},当前线程是{0},返回的参数是 {2}", Thread.CurrentThread.ManagedThreadId,t, result);
return result;
}; AsyncCallback asyncCallback = t =>
{
string res = func.EndInvoke(t);
Console.WriteLine("获取到返回值:{0}", res);
}; func.BeginInvoke("异步参数", asyncCallback, null); Console.WriteLine("----------主程序结束,线程ID是{0}-----------------", Thread.CurrentThread.ManagedThreadId); Console.Read();
}
最新文章
- C#开发微信公众平台-就这么简单(附Demo)
- POJ 1860(spfa)
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- 【转】KMP算法
- 使用AIDL远程调用服务中的方法
- Android SharedPreferences使用以及原理详解
- 第五周技术博客发表 web 网页开发
- python 计算apache进程占用的内存大小以及占物理内存的比例
- 如何使用 Java 构建微服务?
- POJ 3378
- 斐波那契数列 Php练手
- tensorflow卷积神经网络-【老鱼学tensorflow】
- Hibernate学习(九)———— 二级缓存和事务级别详讲
- Annotation 标注
- 使用patroni 构建高可用的pg 数据库
- CF1044B Intersecting Subtrees 构造+树论
- LG2521 [HAOI2011]防线修建
- 读取和修改app.config文件
- MESS-配置
- 【MySQL】经典数据库SQL语句编写练习题——SQL语句扫盲
热门文章
- es6中promise ALL Race Resolve Reject finish的实现
- 『TensorFlow』专题汇总
- word中一页中添加两种不同的页码
- Android proguard混淆签名打包出现";android proguard failed to export application";解决方案
- 使用EasyPOI导出excel示例
- 模仿WC.exe的功能实现--node.js
- struts1与struts2的区别。
- flex布局设置width无效
- 我的主博客在CSDN,这里只有部分文章,这是地址https://blog.csdn.net/z979451341
- Trojan.Backdoor分析