Advertisement
Guest User

ThreadContext

a guest
May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1.     public static class AlbatrossContext
  2.     {
  3.         [ThreadStatic]
  4.         private static ISimpleMessageSender<string> _output;
  5.  
  6.         public static ISimpleMessageSender<string> Output
  7.         {
  8.             get
  9.             {
  10.                 if (_output == null)
  11.                 {
  12.                     _output = SimpleMessageSender.ConsoleLogger("");
  13.                 }
  14.                 return _output;
  15.             }
  16.         }
  17.  
  18.  
  19.         public static IDisposable SetOutput(ISimpleMessageSender<string> newOutput)
  20.         {
  21.             var threadId = Thread.CurrentThread;
  22.  
  23.             var temp = _output;
  24.             _output = newOutput;
  25.  
  26.             return Disposable.Create(
  27.                 () =>
  28.                 {
  29.                     var returnThreadId = Thread.CurrentThread;
  30.                     if(returnThreadId != threadId)
  31.                         throw new DebugException("Do not switch threads inside Using");
  32.  
  33.                     _output = temp;
  34.                 });
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement