using System;
using System.Threading;
using System.Threading.Tasks;
namespace DelegateExample
{
class Program
{
static void Main()
{
Student stu1 = new Student() { ID = 1, PenColor = ConsoleColor.Yellow };
Student stu2 = new Student() { ID = 2, PenColor = ConsoleColor.Red };
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Green };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
/*//第一种同步调用
stu1.DoHomework();
stu2.DoHomework();
stu3.DoHomework();
//第二种同步调用
action1.Invoke();
action2.Invoke();
action3.Invoke();
//使用委托进行隐式的异步调用
action1.BeginInvoke(null, null);
action2.BeginInvoke(null, null);
action3.BeginInvoke(null, null);
//使用Thread进行显式异步调用 第一种方法
Thread thread1 = new Thread(new ThreadStart(stu1.DoHomework));
Thread thread2 = new Thread(new ThreadStart(stu2.DoHomework));
Thread thread3 = new Thread(new ThreadStart(stu3.DoHomework));
thread1.Start();
thread2.Start();
thread3.Start();*/
//使用Task进行显式异步调用 第二种方法
Task task1 = new Task(new Action(stu1.DoHomework));
Task task2 = new Task(new Action(stu2.DoHomework));
Task task3 = new Task(new Action(stu3.DoHomework));
task1.Start();
task2.Start();
task3.Start();
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Main thread{0}", i);
Thread.Sleep(1000);
}
}
}
class Student
{
public int ID { get; set; }
public ConsoleColor PenColor { get; set; }
public void DoHomework()
{
for (int i = 0; i < 5; i++)
{
Console.ForegroundColor = this.PenColor;
Console.WriteLine("student {0} doing homework {1} hours", this.ID, i);
Thread.Sleep(1000);
}
}
}
}