title: Windows Impersonation and TPL
body: <p>I am using <code>WindowsIdentity.Impersonate</code> in order to read from remote registry. However, the actual code that is reading registry located in <code>Task</code> delegates, so it may run on different threads. My question is whether it is legit to impersonate in UI thread and do the elevated job in Thread Pool?</p>
  
  <p>UPDATE:</p>
  
  <pre><code>// get userHandle from LogonUser(...)    
  Task task = Task.Run(() =&gt; 
  {
      using (impersonationContext = WindowsIdentity.Impersonate(userHandle))
      {
          // Do stuff here
          if (impersonationContext != null)
          {
              impersonationContext.Undo();
          }
      }
  }
  </code></pre>