C#委托事件实例
public delegate void TimeoutHandler(int connectID);
public class UserOnLineChecker
{
public event TimeoutHandler SomeConnectionTimeOuted;
private Timer timerForCheckOnLine;
public UserOnLineChecker()
{
this.SomeConnectionTimeOuted = null;
}
private void OnLineCheckAction(object state)
{
if (this.SomeConnectionTimeOuted != null)
{
this.SomeConnectionTimeOuted(state as int);
}
}
public void Start()
{
this.timerForCheckOnLine = new Timer(new TimerCallback(this.OnLineCheckAction), null, 3* 60000, 3 * 60000);
}
}
调用:
public class UserLogin
{
private UserOnLineChecker tcpUserOnLineChecker;
public UserLogin()
{
this.tcpUserOnLineChecker = new UserOnLineChecker();
//this.tcpUserOnLineChecker.Initialize(this.onLineCheckSpan);
this.tcpUserOnLineChecker.SomeConnectionTimeOuted += new TimeoutHandler(this.tcpUserOnLineChecker_SomeConnectionTimeOuted);
}
private void tcpUserOnLineChecker_SomeConnectionTimeOuted(int connectID)
{
//处理回调事件
}
public void Start()
{
this.tcpUserOnLineChecker.Start();//调用UserOnLineChecker类中委托注册事件方法
}
}
邮箱:steven9801@163.com
QQ: 48039387