I'm not sure, but I think you confuse the "cancellation request" with the "termination or interruption of the thread / task". These are two completely different things. According to the Canellation description in Managerd threads, the functionality provided allows you to send something like a signal, indicating that the operation being performed should be stopped.
How and if you react to this signal - as a programmer - is up to you.
In your example, you started a new task
var task = Task.Factory.StartNew(() => { Console.WriteLine("Starting task..."); var t = new System.Net.Sockets.TcpClient(); var buffer = new byte[t.ReceiveBufferSize]; t.Connect(new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 1337)); Console.WriteLine("Recieving..."); t.Client.Receive(buffer); Console.WriteLine("Finished Recieving..."); return true; }, cts.Token);
which does not handle cancellation, and is not suitable for this. Channeling will be used, for example, if you want to break out of a loop with many iterations, so you should check each iteration if the CancellationToken.IsCancellationRequested value is set to true or not. If so, you can respond accordingly.
What you want is to interrupt the thread behind the current task, which, in my opinion, is possible only by creating a new instance of the Thread class for yourself and handle the interrupt accordingly.
Markus safar
source share