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!