1. 定义委托

 

public delegate void TcpConnected(Object sender, ref object o);

 

 

2. 定义事件

 

public event TcpConnected Connected;

 

 

3. 挂载事件

 

if (Connected != null)
Connected(
this, ref s.obj);

 

 

4. 绑定通知函数

 

serv = new TcpServer(TCPLISTENPORT, IPAddress.Any);
serv.Connected += new TcpConnected(Connected); //当有一个用户连接

 

 

5.执行通知函数

 

代码
// A client attached to the tcp port 一个客户端连接上这个端口
private void Connected(object sender, ref object t)
{
lock (this)
//lock 确保当一个线程位于代码的临界区时,另一个线程不进入临界区。
//如果其他线程试图进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放。
{
t
= 0;
iConnectionCount
++;

if (iConnectionCount == 1)
{
ConnectionReady.Set();
}
}
}

 

posted on 2010-07-16 12:57  sn_wolf  阅读(652)  评论(0编辑  收藏  举报