事件调用的一些小问题

xiaotuzi 2009-01-26 08:35:31
using System;
using System.Collections.Generic;
using System.Text;

delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();

class Worker
{
public void DoWork()
{
Console.WriteLine("工作: 工作开始");
if (started != null) started();

Console.WriteLine("工作: 工作进行中");
if (progressing != null) progressing();



Console.WriteLine("“工作: 工作完成”");
if (completed != null)
{
foreach (WorkCompleted wc in completed.GetInvocationList())//GetInvocationList是啥啊?
{
wc.BeginInvoke(new AsyncCallback(WorkGraded), wc);//这句话啥意思?asynccallbakc是啥东西?
}
}

}

private void WorkGraded(IAsyncResult res) {
WorkCompleted wc = (WorkCompleted)res.AsyncState;//这句话啥意思?res.AsyncState是啥意思?
int grade = wc.EndInvoke(res);//这句话啥意思?
Console.WriteLine("工人的工作得分=" + grade);
}




public event WorkStarted started;
public event WorkProgressing progressing;
public event WorkCompleted completed;


}

class Boss
{

public int WorkCompleted()
{
System.Threading.Thread.Sleep(3000);
Console.WriteLine("Better..."); return 6; /* 总分为10 */
}


}

class Universe
{
static void WorkerStartedWork()
{
Console.WriteLine("Universe notices worker starting work");
}


static int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Universe is pleased with worker's work");
return 7;
}



static void Main()
{


Worker peter = new Worker();
Boss boss = new Boss();
peter.completed += new WorkCompleted(boss.WorkCompleted);
peter.started += new WorkStarted(Universe.WorkerStartedWork);
peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);

peter.DoWork();

Console.WriteLine("Main: 工人工作完成");
Console.ReadLine();



}
}





...全文
95 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaotuzi 2009-01-28
  • 打赏
  • 举报
回复
谢谢,结贴给分
xiaotuzi 2009-01-27
  • 打赏
  • 举报
回复
谢谢上面的大大,偶不懂的地方的很多,请问哪里有讲代理和异步编程的相关知识讲的好的资料么?谢谢。
HowkWolf 2009-01-27
  • 打赏
  • 举报
回复
异步调用,上面的仁兄解释的不错,路过!
king19840811 2009-01-27
  • 打赏
  • 举报
回复
MSDN
Quinto 2009-01-26
  • 打赏
  • 举报
回复
建议看一下有关 代理 和 异步编程 相关知识~
Quinto 2009-01-26
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;

delegate void WorkStarted();
delegate void WorkProgressing();
delegate int WorkCompleted();

class Worker
{
public void DoWork()
{
Console.WriteLine("工作: 工作开始");
if (started != null) started();

Console.WriteLine("工作: 工作进行中");
if (progressing != null) progressing();

Console.WriteLine("“工作: 工作完成”");
if (completed != null)
{
foreach (WorkCompleted wc in completed.GetInvocationList())
//Delegate.GetInvocationList 方法
//public virtual Delegate[] GetInvocationList()
//返回值:委托构成的数组,表示当前委托的调用列表。
{
wc.BeginInvoke(new AsyncCallback(WorkGraded), wc);
//异步调用wc这个代理,WorkGrade是回调函数,wc是传给回调函数的对象(类似与参数)
}
}

}

private void WorkGraded(IAsyncResult res)
{
WorkCompleted wc = (WorkCompleted)res.AsyncState;//回调函数接受调用函数传给他的对象,就是刚才的wc
int grade = wc.EndInvoke(res);//故名思议:结束调用
Console.WriteLine("工人的工作得分=" + grade);
}

public event WorkStarted started;
public event WorkProgressing progressing;
public event WorkCompleted completed;
}

class Boss
{
public int WorkCompleted()
{
System.Threading.Thread.Sleep(3000);
Console.WriteLine("Better..."); return 6; /* 总分为10 */
}
}

class Universe
{
static void WorkerStartedWork()
{
Console.WriteLine("Universe notices worker starting work");
}


static int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Universe is pleased with worker's work");
return 7;
}

static void Main()
{
Worker peter = new Worker();
Boss boss = new Boss();
peter.completed += new WorkCompleted(boss.WorkCompleted);
peter.started += new WorkStarted(Universe.WorkerStartedWork);
peter.completed += new WorkCompleted(Universe.WorkerCompletedWork);

peter.DoWork();

Console.WriteLine("Main: 工人工作完成");
Console.ReadLine();
}
}




111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧