Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
824 views
in Technique[技术] by (71.8m points)

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

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...