SHARE
TWEET

Untitled

a guest Oct 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace singleWorker.Services
  2. {
  3.     public interface ISingleService
  4.     {
  5.         Task<int> GetResult();
  6.     }
  7.  
  8.     public class SingleService : ISingleService
  9.     {
  10.         private object locker = new Object();
  11.  
  12.         private Task<int> innerTask;
  13.  
  14.         public Task<int> GetResult()
  15.         {
  16.             lock (locker)
  17.             {
  18.                 if (innerTask == null || innerTask.Status == TaskStatus.RanToCompletion)
  19.                 {
  20.                     innerTask = Task.Run(() =>
  21.                     {
  22.                         Thread.Sleep(5000);
  23.                         return 11;
  24.                     });
  25.                 }
  26.                 return innerTask;
  27.             }
  28.         }
  29.     }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top