简体   繁体   English

STA线程引发InvalidOperationException

[英]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. 我有一个在我的应用程序中实现HTTP服务器的类,因此我可以接受请求。 The goal of this server is to refresh a graph using an XML sent to the application. 该服务器的目标是使用发送到应用程序的XML刷新图形。

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. 我编写的XML解析器使用了我创建的UserControl,称为NewMeshNode,它具有一些属性和一对附加在同一对象中的图像。 The problem comes when the parser arrives to the point of creating a new NewMeshNode object. 当解析器到达创建新的NewMeshNode对象的地步时,问题就来了。

As the NewMeshNode object has graphic parts, I use delegates and have changed the http server thread apartment state to be STA. 由于NewMeshNode对象具有图形部分,因此我使用委托并将http服务器线程单元状态更改为STA。

Here I initialize the local http server: 在这里,我初始化本地http服务器:

 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: 这是我要求解析器使用收到的XML创建列表的方式:

 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: 这是解析器的一部分,其中我创建了一个新的NewMeshNode对象以使用它:

 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: 这是NewMeshNode类:

public partial class NewMeshNode : UserControl { 公共局部类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." 当调试器进入构造函数时,VS总是抛出InvalidOperation异常,并显示以下消息:“调用线程必须是STA,因为许多UI组件都需要STA。”

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 /该调用线程必须保持稳定,因为许多用户界面组件需要此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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