Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         private static Exception _exceptions;
  13.         private static int _result;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             if (Enter())
  18.             {
  19.                 Func<int> _func = () =>
  20.                 {
  21.                     return 5;
  22.                 };
  23.  
  24.                 CancellationTokenSource cts = new CancellationTokenSource();
  25.                 cts.Cancel();
  26.                 Task.Run(_func, cts.Token)
  27.                     .ContinueWith((antecedent) =>
  28.                     {
  29.                         try
  30.                         {
  31.                             antecedent.Wait(cts.Token);
  32.                             Interlocked.Exchange(ref _result, antecedent.Result);
  33.                         }
  34.                         catch (AggregateException e)
  35.                         {
  36.                             Interlocked.Exchange(ref _exceptions, e);
  37.                         }
  38.                         catch (OperationCanceledException)
  39.                         {
  40.                             ResetState();
  41.                         }
  42.                         catch (Exception e)
  43.                         {
  44.                             Interlocked.Exchange(ref _exceptions, new AggregateException(e));
  45.                         }
  46.                         finally
  47.                         {
  48.                             Exit();
  49.                         }
  50.                     });
  51.             }
  52.         }
  53.  
  54.         private static void Exit()
  55.         {
  56.             Console.WriteLine("Hit finally");
  57.         }
  58.  
  59.         private static void ResetState()
  60.         {
  61.             return;
  62.         }
  63.  
  64.         private static bool Enter()
  65.         {
  66.             return true;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement