0

I have a function in my controller class - that is working with xml format file something like

public URI convertXML(Uri pathToFile)
{
  // conversion goes here
}

and I dlike to run it as

[STAThread]
   public URI convertXML(Uri pathToFile)
    {
      // conversion goes here
    }

something like

 public ActionResult DoTheThingsRight()
 {
      //...
      var resultingURI = [as STAThread] convertXML(filePath); 
 }

I have found How to run something in the STA thread? But I dont understand what is GetFooFromAsyncResult in this sample, so perhaps there is another easy way to do this?

Community
  • 1
  • 1
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • Step back a second - *why* do you want to run this code on an STA thread? If it's because something's throwing an error/exception and saying it has a problem with not being on an STA thread, it probably means that the component/function exposing this error just plain won't work well in a service context. Trying to force yourself onto an STA thread won't (generally) fix the larger problem. – Damien_The_Unbeliever Nov 18 '15 at 09:52
  • a DependencyObject that I am trying to parse (Canvas) is required to be created in a Single Thread Affinity context. I should mark the thread as STAThread – curiousity Nov 18 '15 at 09:55
  • [Single-Thread *Apartments*](https://msdn.microsoft.com/en-us/library/windows/desktop/ms680112(v=vs.85).aspx) are a COM concept. You should not take lightly marshaling and unmarshaling COM objects between threads. Perhaps can you achieve the desired result while leaving COM out of the picture? Please read [Understanding and Using COM Threading Models](https://msdn.microsoft.com/library/ms809971.aspx) – Remus Rusanu Nov 18 '15 at 10:02
  • 1
    [This Canvas](https://msdn.microsoft.com/en-gb/library/system.windows.controls.canvas(v=vs.110).aspx)? It's not been designed to work in a service context. – Damien_The_Unbeliever Nov 18 '15 at 10:02
  • The [STAThread] attribute is only valid on the Main() entrypoint of a program. STA is a property of a thread, it makes a **promise**. You must implement the promise, you must never block the thread and run a dispatcher loop (Application.Run). Breaking the promise causes deadlock. And various .NET classes to complain, especially in WPF. A service is a pretty hostile place for WPF classes, maybe you ought to use Bitmap. – Hans Passant Nov 18 '15 at 10:32

0 Answers0