/*皮特的故事*/
using System;
using System.Collections.Generic;
using System.Text;
namespace Delegate
{
public class Class1
{
}
delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();
class Worker
{
//申明三個代理
//public WorkStarted wsStarted;
//public WorkProgressing wsProgressing;
//public WorkCompleted wsCompleted;
//修改為事件就可以動態加載
public event WorkStarted wsStarted;
public event WorkProgressing wsProgressing;
public event WorkCompleted wsCompleted;
public void DoWork()
{
Console.WriteLine("工作:工作開始");
if (wsStarted != null)
wsStarted();
Console.WriteLine("工作:工作進行中");
if (wsProgressing != null)
wsProgressing();
Console.WriteLine("工作:工作結束!");
Console.WriteLine("你在等待結果時候可以做自己的事!");
if (wsCompleted != null)
{
//Console.WriteLine("工人工作的得分=" + wsCompleted());
//通過輪詢監聽者來獲取所有請求,否則只能看到最後一個
foreach (WorkCompleted wc in wsCompleted.GetInvocationList())
{
#region 非異步做法