資料源于劉鐵猛的C#視訊教程

紅色是主線程,其它三種是三個方法。
右邊三個圖分别是同步調用,異步,異步調用的示意圖。
1 單點傳播委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
action1.Invoke();
action2.Invoke();
action3.Invoke();
}
}
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);
}
}
}
}
2 多點傳播委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
//action1.Invoke();
//action2.Invoke();
//action3.Invoke();
action1 += action2;
action1 += action3;
action1.Invoke();
}
}
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);
}
}
}
}
3 同步直接調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
//第一種 同步直 接調用
stu1.DoHomework();
stu2.DoHomework();
stu3.DoHomework();
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("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);
}
}
}
}
4 同步 單點傳播委托間接調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
//第一種 同步 間接調用
action1.Invoke();
action2.Invoke();
action3.Invoke();
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("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);
}
}
}
}
5 同步 多點傳播委托間接調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
//第一種 同步 多點傳播委托間接調用
action1 += action2;
action1 += action3;
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("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);
}
}
}
}
6 委托 隐式異步調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
Action action1 = new Action(stu1.DoHomework);
Action action2 = new Action(stu2.DoHomework);
Action action3 = new Action(stu3.DoHomework);
//異步 委托間接調用
action1.BeginInvoke(null, null);
action2.BeginInvoke(null, null);
action3.BeginInvoke(null, null);
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("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);
}
}
}
}
7 顯式異步調用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
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();
for (int i = 0; i < 10; i++)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Thread {0}", i);
Thread.Sleep(500);
}
}
}
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);
}
}
}
}
8 task異步線程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Del
{
public delegate double Calc(double x, double y);//委托是類申明時與類平級
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student(){ ID = 1, PenColor = ConsoleColor.Yellow};
Student stu2 = new Student(){ ID = 2, PenColor = ConsoleColor.Green};
Student stu3 = new Student() { ID = 3, PenColor = ConsoleColor.Red };
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.White;
Console.WriteLine("Thread {0}", i);
Thread.Sleep(500);
}
}
}
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);
}
}
}
}