"The calling thread must be STA, because many UI components require this." Error in WPF?

Ershad picture Ershad · Nov 9, 2009 · Viewed 17.5k times · Source

I am creating a xps document as below.

Assembly assembly = Assembly.GetExecutingAssembly();
//read embedded xpsDocument file
Stream helpStream = assembly.GetManifestResourceStream(resourceNameOfContext);
if (helpStream != null)
{
    Package package = Package.Open(helpStream);
    string inMemoryPackageName = "memorystream://" + topicName + ".xps";
    Uri packageUri = new Uri(inMemoryPackageName);
    //Add package to PackageStore
    PackageStore.AddPackage(packageUri, package);
    docXps = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName);
}
return docXps;

When i am trying to get docXps.GetFixedDocumentSequence(); I am getting the above error. Can anyone help?

Thanks,

Answer

Ray Burns picture Ray Burns · Nov 9, 2009

Your problem has nothing to do with the code surrounding the creation or use of the XPS document. It has everything to do with what thread you are running under.

You will receive the The calling thread must be STA, because many UI components require this error whenever any of the following are attempted on a MTA thread:

  • You construct any object derived from FrameworkElement (including Controls and Panels)
  • You construct any object derived from BitmapEffect
  • You construct any object derived from TextComposition
  • You construct any object derived from HwndSource
  • You access the current InputManager
  • You access the primary KeyboardDevice, StylusDevice, or TabletDevice
  • You attempt to change the focus on a FrameworkContentElement
  • You provide mouse, keyboard or IME input to any control that accepts text input
  • You make WPF content visible or update its layout
  • You manipulate the visual tree in such a way as to cause a re-evaluation for rendering
  • Several other changes, mostly having to do with display and input

For example, I received this error last year when I tried to deserialize some XAML that contained <Button> and other WPF objects from within a WCF service. The problem was simple to solve: I just switch to a STA thread to do the processing.

Obviously most work with XPS documents will trigger one or more of the above conditions. In your case I suspect that GetFixedDocumentSequence ends up using TextComposition or one of its subclasses.

No doubt the my solution of switching to a STA thread will also work for you, but first you need to figure out how your code that works with XpsDocuments is getting executed run from a MTA thread. Normally any code from from the GUI (eg a button press) is automatically run in a STA thread.

Is it possible that your code that manipulates XPS Documents may be being executed without a GUI? From a user-created thread? From a WCF service or a web service? From an ASPX page? Track that down and you'll probably find your solution.

If that doesn't work, let us know the details of the path through which GetFixedDocumentSequence is called, so we can diagnose it. The directly surrounding code isn't nearly as important as the call stack and how it is originally being invoked. If it is hard to explain you probably should add a call stack to prevent misunderstandings and help us diagnose the problem further, or tell you how to start a STA thread in your particular situation.