1 申明一个自定义的类并且继承事件的基类
public class ClientSocketModelConnectedEvent:EventArgs { private string param; public string Data { get { return param; } set { value = param; } } public ClientSocketModelConnectedEvent(string data) { this.param = data; } }
2 申明委托
public delegate void delConnectedEvent(object sender, ClientSocketModelConnectedEvent _ClientSocketModelConnectedEvent);
3 在要使用的类中定义事件
//定义事件,当客户端成功连接server后就触发此事件 public event delConnectedEvent ConnectedServer;
4 在动作发生的时候触发事件
private void ConnectedReceive(IAsyncResult ar) { CurrnetClientSocket = ar.AsyncState as Socket; CurrnetClientSocket.EndReceive(ar); string data = Encoding.UTF8.GetString(bufferSize); if (ConnectedServer != null) { this.ConnectedServer(this, new ClientSocketModelConnectedEvent(data)); } }
时间: 2024-10-10 05:21:08