Advertisement
Guest User

Untitled

a guest
Aug 11th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1.  
  2. private readonly CancellationTokenSource s_cts = new CancellationTokenSource();
  3.  
  4.  public async Task Test()
  5.     {
  6.         Trace.WriteLine("Waiting started.");
  7.  
  8.         try
  9.         {
  10.             s_cts.CancelAfter(3000);
  11.             await Task.Run(() => Sleep());
  12.         }
  13.         catch (OperationCanceledException ex)
  14.         {
  15.             Trace.WriteLine("Tasks cancelled: timed out.");
  16.         }
  17.         finally
  18.         {
  19.             s_cts.Dispose();
  20.         }
  21.  
  22.         Trace.WriteLine("Waiting ending.");
  23.     }
  24.  
  25.  
  26.     public void Sleep()
  27.     {
  28.         var i = 0;
  29.         while (i < 10)
  30.         {
  31.             Thread.Sleep(1000);
  32.             i += 1;
  33.             Trace.WriteLine(i.ToString());
  34.         }
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement