概要
给线程传递一个回调函数,需要先转换成委托。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 线程关系实验
{
delegate void Funt();
class Program
{
static void Main(string[] args)
{
Console.WriteLine("线程关系实验");
Program program = new Program();
Thread.CurrentThread.Name = "主线程";
program.mian();
Console.ReadKey();
}
void mian() {
Thread thread1 = new Thread(fun);
thread1.Name = "子线程";
Funt CallBack = new Funt(fun2); //把方法赋值给委托
thread1.Start(CallBack);
}
void fun(Object f) {
Console.WriteLine(Thread.CurrentThread.Name);
Funt CallBack = f as Funt;//强转为委托
CallBack();
}
void fun2() {
Console.WriteLine(Thread.CurrentThread.Name);
}
}
}
运行

5023

被折叠的 条评论
为什么被折叠?



