如何 事件触发

色郎中 2014-01-20 01:30:20


class Program
{
public delegate void OnFileChange();//定义委托
public class FileListen
{
public event OnFileChange OnFileChangeEvent;//定义事件
public void test()
{
for (int i = 1; i <= 100; i++)
{
OnFileChangeEvent(); //事件发生

}
}
}
}


static void Main(string[] args)
{
FileListen fl = new FileListen();
fl.OnFileChangeEvent += OutPut;//绑定具体方法到事件
fl.test();
}
private static void OutPut()
{
Console.WriteLine("这是循环到时发生的事件!");
Console.ReadKey();
}
}



代码中,事件只被触发 一次,为啥呢? 如果要触发指定 次数 ,又该如何?

...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
色郎中 2014-01-21
  • 打赏
  • 举报
回复
引用 6 楼 huanggreat 的回复:
哥们,你最近干嘛呢 一天发十多个帖子???
年底不是忙碌 捣鼓捣鼓C# ,以前也偶尔用用,但都没深入,浅尝而已,,很多基本概念到现在都没搞明白... 什么 泛型,事件,委托,反射..... 虽然工作中多数情况下还是用不到,,但是,我觉得有必要要了解了解 这些基础的原理》。。。
lzw099 2014-01-20
  • 打赏
  • 举报
回复
2楼应该 是楼主的意思!学习了
  • 打赏
  • 举报
回复
哥们,你最近干嘛呢 一天发十多个帖子???
BenBenBears 2014-01-20
  • 打赏
  • 举报
回复
ReadKey 方法会一直等待,也就是阻止发出 ReadKey 方法的线程,直到按下某个字符或功能键。按下字符或功能键的同时可以按下 Alt、Ctrl 或 Shift 修改键中的一个或多个。但是,仅按下修改键不会使 ReadKey 方法返回。
ysd_xwl 2014-01-20
  • 打赏
  • 举报
回复
你只调用了一次,当然触发一次了,你要触发几次就调用几次就可以了
leafmao 2014-01-20
  • 打赏
  • 举报
回复
因为Console.ReadKey()需要等待按键按下才行,不按键的话就会一直卡在ReadKey
本拉灯 2014-01-20
  • 打赏
  • 举报
回复
class Program { public delegate void OnFileChange(int num);//定义委托 public class FileListen { public event OnFileChange OnFileChangeEvent;//定义事件 public void test() { for (int i = 1; i <= 100; i++) { OnFileChangeEvent(i); //事件发生 } } } } static void Main(string[] args) { FileListen fl = new FileListen(); fl.OnFileChangeEvent += OutPut;//绑定具体方法到事件 fl.test(); } private static void OutPut(int num) { Console.WriteLine(num); } }
智商余额不足 2014-01-20
  • 打赏
  • 举报
回复
把 Console.ReadKey();去掉

110,536

社区成员

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

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

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