我有一个名为ClientSocketService的类,它在实例化时会创建一个后台线程并开始通过套接字进行侦听。
ClientSocketService.cs
public ClientSocketService(Socket sock) : this()
{
//Assign the Incomign socket to the Socket variable.
_serviceSocket = sock;
//Get and assing the network stream for the Socket.
this._nStream = new NetworkStream(sock);
//Initialize the Reciever Thread.
RecieverThread = new BackgroundWorker();
RecieverThread.DoWork += new DoWorkEventHandler(RecieverThread_StartListening);
RecieverThread.RunWorkerAsync();
}
我在另一个名为 server 的类中创建此类的对象,然后在创建类对象之后,另一个方法将该类添加到 Collection 并引发 ClientAdded 事件处理程序。
private void AcceptClientSocket(Socket sock)
{
//Initialize new ClientSocketService.
ClientSocketService csservice = new ClientSocketService(sock);
//Add the client to the List
this.AddClientToList(csservice);
}
/// <summary>
/// Adds the Client to the List.
/// </summary>
/// <param name="csservice"></param>
private void AddClientToList(ClientSocketService csservice)
{
//Check for any abnormal Disconnections
this.CheckAbnormalDC(csservice);
//Ad the ClientSocketService to the List.
this._clientsocketservices.Add(csservice);
//Raise the Client Added Event Handler.
this.OnClientAdded(new ClientSocketServiceEventArgs(csservice));
}
我现在面临的问题是 ClientSocketService 类中的后台工作程序在调用所有添加的事件处理程序事件后启动。
任何帮助都感激不尽。
谢谢,