代码运行环境Visual Studio 2010 .NET v4.0.30319
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;//线程需要引用
using System.Threading.Tasks;//任务需要引用
namespace DelegateUse
{
/// <summary>
/// 委托是封装了方法(包括静态方法和实例方法)细节的特殊类型,主要适合用在需要把方法作为参数传递给方法并且参数所代表
/// 的具体方法只有在运行时才能确定的场景,比如线程、任务、通用类功能和事件
/// </summary>
class Program
{
static void Main(string[] args)
{
ThreadEx();
TaskEx();
}
#region 在线程中利用委托
/// <summary>
/// 在线程中可以单独执行各类方法,但是具体方法却不确定,所以线程构造函数中利用了委托,允许
/// 调用方法可以灵和选择具体的方法,线程构造函数中支持两种委托类型:
/// ThreadStart:指向无参数无返回值的方法;
/// ParameterizedThreadStart:指向含一个object参数的无返回值的方法。
/// </summary>
private static void ThreadEx()
{
//每个线程执行中通过Thread.Sleep(1000)来保证前一个线程执行完毕再执行下一个线程
Console.WriteLine("**********运行线程1**********");
ThreadStart ts=Method1;
Thread tr1=new Thread(ts);
tr1.Start();
Thread.Sleep(1000);
Console.WriteLine("**********运行线程2**********");
ts += Method2;
Thread tr2 = new Thread(ts);
tr2.Start();
Thread.Sleep(1000);
Console.WriteLine("**********运行线程3**********");
TestClass tc1 = new TestClass("线程对象tc1");
ParameterizedThreadStart pts = Method3;
Thread tr3 = new Thread(pts);
tr3.Start(tc1);
Thread.Sleep(1000);
Console.WriteLine("**********运行线程4**********");
TestClass tc2 = new TestClass("线程对象tc2");
pts += Method4;
Thread tr4 = new Thread(pts);
tr4.Start(tc2);
Thread.Sleep(1000);
}
#endregion
#region 在任务中利用委托
/// <summary>
/// 线程是每次都需要新建一个线程,为了提高效率,.NET提供了更高效任务类,Task不会每次都创建一个操作系统线程,而是从线程池中请求一个线程。
/// 任务Task也是通过委托来传递需要任务操作的指令,当前使用两个系统设定的委托进行任务的初始化,他们是:
/// Action:指向无参数无返回值的方法或指令
/// Action<object>:指向含一个object参数的无返回值的方法或指令
/// </summary>
private static void TaskEx()
{
//此处利用任务的Wait()方法,保证在任务运行完毕后,主进程才退出
Console.WriteLine("**********运行任务1**********");
Action action1 = Method1;
Task tsk =new Task(action1);
tsk.Start();
tsk.Wait();
Console.WriteLine("**********运行任务2**********");
action1 += Method2;
tsk = new Task(action1);
tsk.Start();
tsk.Wait();
Console.WriteLine("**********运行任务3**********");
Action<object> action2 = Method3;
TestClass tc1=new TestClass("任务对象tc1");
Task tsk2 = new Task(action2,tc1);
tsk2.Start();
tsk2.Wait();
Console.WriteLine("**********运行任务4**********");
TestClass tc2 = new TestClass("任务对象tc2");
action2 += Method4;
tsk2 = new Task(action2, tc1);
tsk2.Start();
tsk2.Wait();
}
#endregion
#region 公用方法和类
//调用方法1
private static void Method1()
{
Console.WriteLine("调用方法:Method1");
Console.WriteLine("托管线程ID为:" + Thread.CurrentThread.ManagedThreadId);
}
//调用方法2
private static void Method2()
{
Console.WriteLine("调用方法:Method2");
Console.WriteLine("托管线程ID为:" + Thread.CurrentThread.ManagedThreadId);
}
//调用方法3
private static void Method3(Object o)
{
Console.WriteLine("调用方法:Method3;参数:" + o.GetType() + ";Message属性为:" + (o as TestClass).Message);
Console.WriteLine("托管线程ID为:" + Thread.CurrentThread.ManagedThreadId);
}
//调用方法4
private static void Method4(object o)
{
Console.WriteLine("调用方法:Method4;参数:" + o.GetType() + ";Message属性为:" + (o as TestClass).Message);
Console.WriteLine("托管线程ID为:" + Thread.CurrentThread.ManagedThreadId);
}
//定义测试用对象
class TestClass
{
private string _message;
public string Message
{
get
{
return _message;
}
set
{
_message = value;
}
}
public TestClass(string message)
{
_message = message;
}
}
#endregion
}
}
运行结果: