title: WPF - Trying to open a new window in a Task but receive a "The calling thread must be STA exception"
body: <p>this is my first post so forgive me if I make and mistakes. </p>
  
  <p>I have a Task that returns a string. Within that Task I want to open a new window where the user enters a code. Once the code is entered and the window is closed the Task will return the code. </p>
  
  <p>My code is as follows:</p>
  
  <pre><code> public Task&lt;string&gt; GetLoginCode()
          {
  
              return Task.Run(() =&gt;
              {
                  CodeRequestView view = new CodeRequestView();
                  CodeRequestViewModel viewModel = new CodeRequestViewModel();
                  view.ShowDialog();
                  return viewModel.Code;
              });
  
          }
  </code></pre>
  
  <p>The issue I'm having is when I run my project Im receiving a "The calling thread must be STA, because many UI components require this." exception at the constructor of the CodeRequestView.xmal.cs file.</p>
  
  <p>Some help on how to resolve this would be greatly appreciated.
  Thanks! </p>