阅读目录
一:实例
一:实例
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;namespace ThreadDelegate
class Program { static void Main(string[] args) { Thread thread = SimpleThread.CreateThread(new SimpleThread.MyDelegate(User.DelegateMethod), "从小就犯困"); thread.Start(); thread.Join(Timeout.Infinite); Console.ReadKey(); } }class User
{ //step2定义一个静态方法 public static void DelegateMethod(object obj) { Console.WriteLine("我的名字叫:" + obj); } } class SimpleThread { //step1声明一个委托 public delegate void MyDelegate(object obj);/// <summary>
/// 创建一个用户类 /// </summary> public class User { public object _name;//名字 public MyDelegate mydelegate; /// <summary> /// 得到名字 /// </summary> public void GetName() { mydelegate(_name); } }/// <summary>
/// 创建一个线程 /// </summary> /// <param name="mydelegate"></param> /// <param name="name"></param> /// <returns></returns> public static Thread CreateThread(MyDelegate mydelegate, object name) { User user = new User(); user._name = name; user.mydelegate = mydelegate; Thread thread = new Thread(user.GetName); return thread; } } }