title: STA Threading, Rtf to HTML converter issues
body: <p>I'm using <a href="http://blogs.msdn.com/b/matt/archive/2009/09/28/converting-rtf-to-html.aspx" rel="nofollow">this code</a> to attempt to convert my Rtf text from a <code>RichTextBox</code> in my UI to Html. I have 2 projects in my solution: the UI &amp; application written in MVVM- <code>IsesTextEditor</code> and the pre-written Converter as provided in the link- <code>MarkupConverter</code>.</p>
  
  <p>A btn in my view is bound to a command in my view model which passes the RichTextBox text into the ConvertRtfToHtml method as per the example:</p>
  
  <pre><code> private string ConvertRtfToHtml(string PastedText)
      {
          var thread = new Thread(ConvertRtfInSTAThread);
          var threadData = new ConvertRtfThreadData { RtfText = PastedText };
          thread.SetApartmentState(ApartmentState.STA);
          thread.Start(threadData);
          thread.Join();
          return threadData.HtmlText;
      }
  
      private void ConvertRtfInSTAThread(object rtf)
      {
          var threadData = rtf as ConvertRtfThreadData;
          threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
      }
  
      private class ConvertRtfThreadData
      {
          public string RtfText { get; set; }
          public string HtmlText { get; set; }
      }
  </code></pre>
  
  <p>The <code>MarkupConverter.ConvertRtfToHtml</code> method then calls <code>ConvertRtfToXaml</code> which instantiates a new <code>RichTextBox</code> object:</p>
  
  <pre><code>    public static class RtfToHtmlConverter
      {
       private const string FlowDocumentFormat = "&lt;FlowDocument&gt;{0}&lt;/FlowDocument&gt;";
  
       public static string ConvertRtfToHtml(string rtfText)
       {
          var xamlText = string.Format(FlowDocumentFormat, ConvertRtfToXaml(rtfText));
  
          return HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false);
      }
  
      private static string ConvertRtfToXaml(string rtfText)
      {
          var richTextBox = new RichTextBox();
          if (string.IsNullOrEmpty(rtfText)) return "";
  
          var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
  
          using (var rtfMemoryStream = new MemoryStream())
          {
              using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
              {
                  rtfStreamWriter.Write(rtfText);
                  rtfStreamWriter.Flush();
                  rtfMemoryStream.Seek(0, SeekOrigin.Begin);
  
                  textRange.Load(rtfMemoryStream, DataFormats.Rtf);
              }
          }
  
          using (var rtfMemoryStream = new MemoryStream())
          {
  
              textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
              textRange.Save(rtfMemoryStream, DataFormats.Xaml);
              rtfMemoryStream.Seek(0, SeekOrigin.Begin);
              using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
              {
                  return rtfStreamReader.ReadToEnd();
              }
          }
  
      }
  }
  </code></pre>
  
  <p>At creating the <code>RichTextBox</code> object I'm getting a <code>The calling thread cannot access this object because a different thread owns it.</code> exception.</p>
  
  <p>Can anyone suggest a fix for this? I'm sure it's a relatively simple threading issue but I'm a junior dev &amp; don't have much experience with threading or STA...</p>