.NET 4.5.1: Sembra che non sia possibile annullare un metodo di blocco eseguito all'interno di un'attività che utilizza il timeout predefinito CancellationTokenSource.C#: CancellationToken non annulla il metodo di blocco
class Program
{
static void Main(string[] args)
{
var cts = new System.Threading.CancellationTokenSource();
System.Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
MainAsync(args, cts.Token).Wait();
}
// MainAsync from http://stackoverflow.com/questions/9208921/async-on-main-method-of-console-app
static async Task MainAsync(string[] args, System.Threading.CancellationToken token)
{
Console.WriteLine("Starting MainAsync");
var cts = new System.Threading.CancellationTokenSource(3000);
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);
var success = await task;
Console.WriteLine("Did the task complete succesfuly?", success);
}
}
L'uscita dal sopra Corto, Self Contained, esempio corretto (spero che sia corretto) è:
Starting MainAsync
Starting task...
Recieving...
Perché il compito non cancella, non viene generata un'eccezione?
Utilizzare socket.receive come in questo caso è una chiamata di blocco: è necessario fornire un timeout o un utilizzo asincrono in modo da poter rivalutare il token di annullamento durante l'elaborazione. CancellationToken non terminerà magicamente le chiamate di blocco per te. – Joe