0

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!

DrGrijando
  • 109
  • 2
  • 8
  • 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 – Nitin Sep 03 '13 at 10:31
  • App.localHttpServerThread.Join(); after App.localHttpServerThread.Start(); – Krish Sep 03 '13 at 12:59
  • glad it worked for you.. lets put the answer so others can take benefit from this.. – Nitin Sep 04 '13 at 11:38
  • @nit: Thanks for the link, it helped me to find the solution. – DrGrijando Sep 04 '13 at 11:38
  • @Krish: Your solution didn't work becasue I have many visual elements that don't appear because I keep the thread waiting for a response. – DrGrijando Sep 04 '13 at 11:39
  • @DrGrijando: its ok i expressed my view, may be that may not be worked for you.Any way you got the solution. – Krish Sep 04 '13 at 12:10

1 Answers1

1

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

Nitin
  • 18,344
  • 2
  • 36
  • 53