Guest User

Untitled

a guest
Apr 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 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.     public class RequestList
  9.     {
  10.         Dictionary<Guid, RequestMessage> requests;
  11.         public RequestList()
  12.         {
  13.             requests = new Dictionary<Guid, RequestMessage>();
  14.         }
  15.  
  16.         public void Add(RequestMessage request)
  17.         {
  18.             request.Status = MessageStatus.Pending;
  19.             //Create a cancellation token source, this will be used to cancel a task
  20.             request.RequestCancelToken = new CancellationTokenSource();
  21.             requests.Add(request.MessageID, request);
  22.             //Create and start a task and pass on the cancellation token to it
  23.             var task = Task.Factory.StartNew(() =>
  24.                 //We also pass on the token to the operation for it access the token to check for
  25.                 //cancellation
  26.                 Process(request), request.RequestCancelToken.Token
  27.             );
  28.  
  29.         }
  30.  
  31.         public void Cancel(RequestMessage request)
  32.         {
  33.             var dictRequestValue = (from x in requests
  34.                           where x.Key == request.MessageID
  35.                           select x).First().Value;
  36.  
  37.             dictRequestValue.RequestCancelToken.Cancel();
  38.             dictRequestValue.Status = MessageStatus.Cancelled;
  39.         }
  40.  
  41.         //This method contains the operation being executed by the task in parallel
  42.         private static void Process(RequestMessage request)
  43.         {
  44.             request.Status = MessageStatus.Processing;
  45.             CancellationToken token = request.RequestCancelToken.Token;
  46.  
  47.             //Check if cancellation is requested, using the token passed to the method
  48.             //This check is for the case when cancellation might have been requested even before start of the
  49.             //task
  50.             if (token.IsCancellationRequested)
  51.             {
  52.                 //This throws an OperationCanceledException if the token has had cancellation requested
  53.                 token.ThrowIfCancellationRequested();
  54.             }
  55.  
  56.             //This simulates work
  57.             var sw = new SpinWait();
  58.             for (var i = 0; i < 1000; i++)
  59.             {
  60.                 for (int j = 0; j < 50; j++)
  61.                 {
  62.                     sw.SpinOnce();
  63.                 }
  64.                 //Check for requested cancellation amidst execution logic
  65.                 if (token.IsCancellationRequested)
  66.                 {
  67.                     //This throws an OperationCanceledException if the token has had cancellation requested
  68.                     token.ThrowIfCancellationRequested();
  69.                 }
  70.             }
  71.             //This code is reached if the task gets to complete prior to a cancellation request
  72.             request.Status = MessageStatus.Done;
  73.         }
  74.  
  75.  
  76. }
Add Comment
Please, Sign In to add comment