Advertisement
YuraSidorets

Untitled

Apr 16th, 2024 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. public class SlowRequestController : Controller
  2. {
  3.     private readonly IRepository _repository;
  4.  
  5.     public SlowRequestController(IRepository repository)
  6.     {
  7.         _repository = repository;
  8.     }
  9.  
  10.     [HttpGet("/slowtest")]
  11.     public async Task<IActionResult> Get(CancellationToken cancellationToken)
  12.     {
  13.         await _repository.LongRunningTask(cancellationToken);
  14.  
  15.         return Ok();
  16.     }
  17. }
  18.  
  19. public class Repository : IRepository
  20. {  
  21.     public async Task LongRunningTask(CancellationToken cancellationToken)
  22.     {
  23.    
  24.         // token.ThrowIfCancellationRequested(); or below
  25.         if (token.IsCancellationRequested) // true if RequestAborted by any reason
  26.         {
  27.             Console.WriteLine("Task cancelled");
  28.             return;
  29.         }
  30.  
  31.         // slow cancellable work
  32.         await Task.Delay(10_000, cancellationToken);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement