苏州做外贸网站,四川华泰建设集团网站,网站开发与设计总结,三室二厅20万全款作用#xff1a;
事件#xff08;Event#xff09;基本上说是一个用户操作#xff0c;如按键、点击、鼠标移动等等#xff0c;或者是一些提示信息#xff0c;如系统生成的通知。应用程序需要在事件发生时响应事件。例如#xff0c;中断。C# 中使用事件机制实现线程间的…作用
事件Event基本上说是一个用户操作如按键、点击、鼠标移动等等或者是一些提示信息如系统生成的通知。应用程序需要在事件发生时响应事件。例如中断。C# 中使用事件机制实现线程间的通信。加上了event的权限控制限制权限只允许在事件声明类里面去invoke和赋值不允许外面甚至子类调用。外面可以通过“”、“-”注册事件
public delegate void BoilerLogHandler(string status);
// 基于上面的委托定义事件
public event BoilerLogHandler BoilerEventLog;using System;
namespace SimpleEvent
{using System;/***********发布器类***********/public class EventTest{private int value;public delegate void NumManipulationHandler();public event NumManipulationHandler ChangeNum;protected virtual void OnNumChanged(){if ( ChangeNum ! null ){ChangeNum(); /* 事件被触发 */}else {Console.WriteLine( event not fire );Console.ReadKey(); /* 回车继续 */}}public EventTest(){int n 5;SetValue( n );}public void SetValue( int n ){if ( value ! n ){value n;OnNumChanged();}}}/***********订阅器类***********/public class subscribEvent{public void printf(){Console.WriteLine( event fire );Console.ReadKey(); /* 回车继续 */}}/***********触发***********/public class MainClass{public static void Main(){EventTest e new EventTest(); /* 实例化对象,第一次没有触发事件 */subscribEvent v new subscribEvent(); /* 实例化对象 */e.ChangeNum new EventTest.NumManipulationHandler( v.printf ); /* 注册 */e.SetValue( 7 );e.SetValue( 11 );}}
}