STA thread throws InvalidOperationException

890 views Asked by At

This is the situation:

I have a class that implements a HTTP server in my application, so I can take requests. The goal of this server is to refresh a graph using an XML sent to the application.

The XML parser that I have written uses a UserControl I made, called NewMeshNode, which has some attributes and a pair of images attached in the same object. The problem comes when the parser arrives to the point of creating a new NewMeshNode object.

As the NewMeshNode object has graphic parts, I use delegates and have changed the http server thread apartment state to be STA.

Here I initialize the local http server:

App.localHttpServer = new MyHttpServer(8080);
App.localHttpServerThread = new Thread(new ThreadStart(App.localHttpServer.listen));
App.localHttpServerThread.SetApartmentState(ApartmentState.STA);
App.localHttpServerThread.Name = "HttpServerThread";
App.localHttpServerThread.Start();

This is how I ask the parser to create a list with the XML I receive:

public delegate ArrayList delListString(string s); 
.
.
.
delListString del = new delListString(App.parser.GetParameters);
App.nodeInfo = (ArrayList)Dispatcher.CurrentDispatcher.Invoke(del, tokens[0]);

This is the part of the parser where I create a new NewMeshNode object to use it:

public ArrayList GetParameters(string xml)
{
  ArrayList parameters=new ArrayList();
  int sensorCount = 0;

  MemoryStream ms = new MemoryStream();
  ms.Write(Encoding.UTF8.GetBytes(xml), 0, Encoding.UTF8.GetBytes(xml).Length);
  ms.Position = 0;
  byte[] byteArray = ms.ToArray();

  string resul = Encoding.UTF8.GetString(byteArray);

  resul = resul.Substring(resul.IndexOf("\n") + 1);

  byteArray = Encoding.UTF8.GetBytes(resul);
  MemoryStream rms = new MemoryStream(byteArray);

  XmlReaderSettings settings = new XmlReaderSettings();
  settings.IgnoreComments=true;
  settings.IgnoreWhitespace=true;

  XmlReader xmlr = XmlReader.Create(rms, settings);
  xmlr.Read();
  string xmlType = xmlr.Name;
  string currentElement="";
  string secondaryElement = "";
  NewMeshNode node = new NewMeshNode();
  .
  .
  .

And this is the NewMeshNode class:

public partial class NewMeshNode : UserControl {

    public string name = "";
    public string mac = "";
    public string address = "";
    public string state = "";
    public string type = "";
    public int pipeLive = 0;
    public double xOnGraph = 0.0;
    public double yOnGraph = 0.0;
    public string pointsTo = "";
    public ArrayList sensors = new ArrayList();
    public ArrayList oldAddress = new ArrayList();

    public NewMeshNode()
    {
        InitializeComponent();
    }
}

VS always throws an InvalidOperation exception when the debuger enters the constructor, with the message: "The calling thread must be STA, because many UI components require this."

What am I doing wrong?

Thanks in advance!

1

There are 1 answers

0
Nitin On BEST ANSWER

As the main reason is self explanotory that thread should be STA and setting it does not solve your problem, so you can try few tricks mentioned in this article.. like clean build you solution, visual studio settings etc.. http://social.msdn.microsoft.com/Forums/vstudio/en-US/d1e17dc5-ea88-453b-b87f-7154e6c6c75a/the-calling-thread-must-be-sta-because-many-ui-components-require-this