0
請看看下面的代碼取消/中止任務
List<Task> tasks = new List<Task>();
static void Main(string[] args)
{
while (true)
{
Socket serverSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, Helper.PORT));
serverSocket.Listen(0);
while(true)
{
Socket clientSocket = serverSocket.Accept();
Client client = new Client(clientSocket);
Task t = new Task(delegate { client.Handle(); });
t.Start();
tasks.Add(t);
}
}
Client.Handle():
while (true)
{
byte[] buffer = new byte[1024];
clientSocket.Recieve(buffer);
//Do Something...
}
有什麼辦法來取消/放棄的時候有在名單超過10個任務的任務?
非常感謝您的幫助。外部循環監聽傳入連接,內部循環處理這些連接。 –