简体   繁体   中英

STA thread throws InvalidOperationException

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!

因为主要的原因是自我说明线程应该是STA,并且对其进行设置不能解决您的问题,所以您可以尝试本文中提到的一些技巧。.例如干净地构建解决方案,Visual Studio设置等。http:// social .msdn.microsoft.com / Forums / vstudio / zh-CN / d1e17dc5-ea88-453b-b87f-7154e6c6c75a /该调用线程必须保持稳定,因为许多用户界面组件需要此

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM