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();
}
}
}

 

相关文章:

  • 2021-09-11
  • 2022-12-23
  • 2021-12-04
  • 2021-08-06
  • 2021-10-27
  • 2021-12-28
  • 2021-12-19
  • 2021-04-23
猜你喜欢
  • 2022-12-23
  • 2021-12-31
  • 2021-11-15
  • 2021-07-23
  • 2022-02-03
  • 2022-12-23
相关资源
相似解决方案