インスタンス化されたときにバックグラウンド スレッドを作成し、ソケットを介してリッスンを開始する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();
}
このクラスのオブジェクトをサーバーと呼ばれる別のクラスで作成します。クラス オブジェクトが作成された後、別のメソッドがクラスを 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 クラスのバックグラウンド ワーカーが開始することです。
どんな助けでも大歓迎です。
ありがとう、