Task types and the Task type inherited from it . These types are wrappers for asynchronous operations; they allow to abstract from such technical details as, for example, flows and to synchronize asynchronous operations with each other. CancellationTokenSource - creates cancellation markers (the Token property) and processes requests for cancellation of an operation (overloaded Cancel / CancelAfter ).CancellationToken - cancellation marker; allows you to track cancellation requests in several ways: by polling the IsCancellationRequested property, by registering the callback function (via the overloaded Register method), by waiting on the synchronization object (the WaitHandle property).OperationCanceledException - an exception whose throwing by convention means that the request to cancel an operation has been processed and the operation must be considered canceled. The preferred way to generate an exception is to call the CancellationToken. ThrowIfCancellationRequested method CancellationToken. ThrowIfCancellationRequested CancellationToken. ThrowIfCancellationRequested .CancellationToken is standard for the TPL β there are overloads of methods that accept CancellationToken , exceptions OperationCanceledException are handled in a special way, etc. However, as in any other API, there are subtleties, tricks, best practices. CancellationToken is cooperative, because it requires the coordinated support of the cancellation functionality from all three layers. A typical code that performs an asynchronous operation with support for cancellation looks like this: CancellationTokenSource cts; void Start() { cts = new CancellationTokenSource(); // var task = Task.Run(() => SomeWork(cts.Token), cts.Token); // // // ... } int SomeWork(CancellationToken cancellationToken) { int result; while (true) { // - ... // ... , cancellationToken.ThrowIfCancellationRequested(); } // return result; } void Cancel() { // cts.Cancel(); } Start and Cancel functions is the outer layer. It initiates an asynchronous operation by calling the Task.Run method. In doing so, he passes the CancellationToken into two places at once: into the inner layer that directly performs the operation (the SomeWork method) and into the infrastructure layer (the second argument of the Task.Run function). The Cancel function requests that the operation be canceled.SomeWork function) besides executing the payload periodically checks whether cancellation is requested and, if necessary, throws an OperationCanceledException exception indicating that the cancellation request has been processed.Task.Run , it checks whether cancellation is requested. If so, the function does not even run. Secondly, it handles in a special way the exception OperationCanceledException , informing about the cancellation of the operation.CancellationTokenSource.Cancel method (or after the timeout specified when calling the CancellationTokenSource constructor / CancellationTokenSource.CancelAfter method has been called), the CancellationTokenSource.CancelAfter object changes to the canceled state. The transition to the canceled state can occur exactly once. It is impossible to change your mind and make a canceled CancellationTokenSource not canceled, and repeated calls to Cancel ignored.CancellationToken.Register method are called when switching to the canceled state, the CancellationToken.Register property starts returning true , and a call to the CancellationToken.ThrowIfCancellationRequested method will generate an OperationCanceledException exception.ThrowIfCancellationRequested method if the operation can be interrupted immediately. If you need to perform additional actions before IsCancellationRequested (for example, release used resources), then the verification code refers to the IsCancellationRequested property. If it is true , the resource is cleared and an OperationCanceledException exception is thrown (again, by calling the ThrowIfCancellationRequested method or manually).Task.IsCompleted == true ), but it will not be considered canceled ( Task.IsCanceled == false ).CancellationToken has a CanBeCanceled property. If it returns false , the cancellation request is guaranteed not to be and checks can be omitted. CanBeCanceled is false if the CancellationToken received using the static CancellationToken.None property or created by the default constructor or constructor with the parameter set to false .Task.IsCanceled property returns true ), the following is necessary:OperationCanceledException exception, to the constructor of which the CancellationToken must be passed.CancellationToken must be passed to the method that creates and runs the task.IsCancellationRequested property of a CancellationToken should return true . var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; //... Task.Run(() => { //... if (cancellationToken.IsCancellationRequested) throw new OperationCanceledException(); // CancellationToken //... }, cancellationToken); CancellationToken not passed to the OperationCanceledException constructor. That is why it is preferable to use the CancellationToken.ThrowIfCancelationRequested method and not to generate an OperationCanceledException manually. var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; //... Task.Run(() => { //... cancellationToken.ThrowIfCancellationRequested(); //... }/* CancellationToken */); Task.Run not passed to the Task.Run method. var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; var cts2 = new CancellationTokenSource(); var cancellationToken2 = cts2.Token; //... var task = Task.Run(() => { //... cancellationToken2.ThrowIfCancellationRequested(); // «» CancellationToken //... }, cancellationToken); CancellationToken are used, obtained from various CancellationTokenSource 's. var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; //... var task = Task.Run(() => { //... if (!cancellationToken.IsCancellationRequested) // throw new OperationCanceledException(cancellationToken); //... }, cancellationToken); OperationCanceledException generated here, although no cancellation was requested.OperationCanceledException exception will be treated inside the TPL not as a cancel signal, but as a normal exception. As a result, the task will not end with the cancellation, but with an error ( Task.IsCancelled == false , Task.IsFaulted == true ).Task.ContinueWith method. The task created by the Task.ContinueWith method is called the continuation task, and the task for which the method was called is the predecessor task. The continuation task waits for the completion of the predecessor task, and then it executes var antecedentTask = Task.Run(() => Console.Write("Hello, ")); var continuationTask = antecedentTask.ContinueWith(_ => Console.Write("world!")); // `Hello, world!` Task.ContinueWith method has overloads that allow you to associate with the created task CancellationToken . var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; var antecedentTask = Task.Run(() => Thread.Sleep(5000)); var continuationTask = antecedentTask.ContinueWith( _ => {}, cancellationToken); Thread.Sleep(1000); cts.Cancel(); Console.WriteLine(antecedentTask.IsCompleted); // False Console.WriteLine(continuationTask.IsCompleted); // True TaskContinuationOptions.LazyCancellation flag TaskContinuationOptions.LazyCancellation specify that a lazy cancellation is required. In this case, the continuation task will not be canceled (and will not end) until the predecessor task is completed: var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; var antecedentTask = Task.Run(() => Thread.Sleep(5000)); var continuationTask = antecedentTask.ContinueWith( _ => {}, cancellationToken, TaskContinuationOptions.LazyCancellation, TaskScheduler.Default); Thread.Sleep(1000); cts.Cancel(); Console.WriteLine(antecedentTask.IsCompleted); // False Console.WriteLine(continuationTask.IsCompleted); // False CancellationToken . In TaskContinuationOptions there are a number of flags that allow you to specify in which cases the continuation task should be launched. For example, for the predecessor task, you can create two continuation tasks β one for the case when the predecessor task completes normally, the second for the case when an error / cancellation occurs in the predecessor task. After the completion of the predecessor task, only one of the continuation tasks will start, and the second will be canceled: var antecedentTask = Task.Run(() => {}); var continuationTask1 = antecedentTask.ContinueWith( _ => {}, TaskContinuationOptions.OnlyOnRanToCompletion); var continuationTask2 = antecedentTask.ContinueWith( _ => {}, TaskContinuationOptions.NotOnRanToCompletion); try { Task.WaitAll(continuationTask1, continuationTask2); } catch{} Console.WriteLine(continuationTask1.IsCanceled); // False Console.WriteLine(continuationTask2.IsCanceled); // True void and Task / Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } types can be used as return values Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); }Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); }Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); }Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); }Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); }Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Task. , async- (Task / Task), , . ,
Task<int> task = Task .Delay(TimeSpan.FromSeconds(5)) .ContinueWith(_ => { while (true) { // - } return 42; });
async- :
Task<int> task = SomeWork(); async Task<int> SomeWork() { await Task.Delay(TimeSpan.FromSeconds(5)); while (true) { // - } return 42; }
, Task.Run , Task.Factory.StartNew , . SomeWork int . , SomeWork Task.
, async-, , , . , :
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = Task .Delay(TimeSpan.FromSeconds(5), cancellationToken) .ContinueWith(_ => { while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }, cancellationToken);
var cts = new CancellationTokenSource(); var cancellationToken = cts.Token; Task<int> task = SomeWork(cancellationToken); async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); /* 1 */ await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); cancellationToken.ThrowIfCancellationRequested(); /* 2 */ while (true) { // - cancellationToken.ThrowIfCancellationRequested(); } return 42; }
, async-, , CancellationToken ( CancellationToken async- CancellationToken ). :
async-, async- . (, Task.Run Task.ContinueWith ) . 1 2. , async-, . OperationCanceledException . CancellationToken , , .. . , , CancellationToken . , Task.Run , :
static async Task<int> SomeWork(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); throw new OperationCanceledException(cancellationToken); throw new OperationCanceledException(); throw new OperationCanceledException(CancellationToken.None); throw new OperationCanceledException(new CancellationToken(true)); throw new OperationCanceledException(new CancellationToken(false)); }
Task.IsCompleted true . ( Task.Status == TaskStatus.RanToCompletion ), ( Task.Status == TaskStatus.Faulted ; Task.IsFaulted == true ) ( Task.Status == TaskStatus.Canceled ; Task.IsCanceled == true ). .
-. TaskContinuationOptions - -:
var task = Task.Run(() => 42); task.ContinueWith( t => Console.WriteLine("Result: {0}", t.Result), TaskContinuationOptions.OnlyOnRanToCompletion); task.ContinueWith( _ => Console.WriteLine("Canceled"), TaskContinuationOptions.OnlyOnCanceled); task.ContinueWith( t => Console.WriteLine("Error: {0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Task.Wait Task.Result. c , . . -, β AggregateException . AggregateException , ( AggregateException ). -, , , AggregateException TaskCanceledException , OperationCanceledException , :
var task = Task.Run(() => { /* - */ }); try { task.Wait(); Console.WriteLine("Success"); } catch (AggregateException ae) { try { ae.Flatten().Handle(e => e is TaskCanceledException); Console.WriteLine("Cancelled"); } catch (AggregateException e) { Console.WriteLine("Error: {0}", e); } }
async- await . AggregateException , OperationCanceledException ; :
var task = Task.Run(() => { /* - */ }); try { await task; Console.WriteLine("Success"); } catch (OperationCanceledException) { Console.WriteLine("Cancelled"); } catch (Exception e) { Console.WriteLine("Error: {0}", e); } Source: https://habr.com/ru/post/168669/
All Articles