public class Program
{
//定义一个为委托
public delegate void Entrust(string str);
static void Main(string[] args)
{
Entrust callback = new Entrust(CallBack); //把方法赋值给委托
Thread th = new Thread(Fun);
th.IsBackground = true;
th.Start(callback);//将委托传递到子线程中
Console.ReadKey();
}
private static void Fun(object obj) {
//注意:线程的参数是object类型
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("子线程循环操作第 {0} 次",i);
Thread.Sleep(500);
}
Entrust callback = obj as Entrust;//强转为委托
callback("我是子线程,我执行完毕了,通知主线程");
//子线程的循环执行完了就执行主线程的方法
}
//主线程的方法
private static void CallBack(string str) {
Console.WriteLine(str);
}
}
public class Program
{
//定义一个为委托
public delegate void Entrust(string str);
static void Main(string[] args)
{
Action<string> callback = ((string str) => { Console.WriteLine(str); });
//Lamuda表达式
Thread th = new Thread(Fun);
th.IsBackground = true;
th.Start(callback);
Console.ReadKey();
}
private static void Fun(object obj) {
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("子线程循环操作第 {0} 次",i);
Thread.Sleep(500);
}
Action<string> callback = obj as Action<string>;
callback("我是子线程,我执行完毕了,通知主线程");
}
}
//上面的Lamuda表达式也可以回城匿名函数
//Action<string> callback = delegate(string str) { Console.WriteLine(str); };
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有