C# 无法访问多线程WPF应用程序中的XML节点
我尝试从WPF应用程序中的不同线程访问XMLNode的属性。但是,来自线程的第一次访问(不是XMLDocument的创建线程)会导致System.InvalidOperationException类型的异常 详情如下: 调用线程无法访问此对象,因为另一个 线程拥有它 这是我的密码C# 无法访问多线程WPF应用程序中的XML节点,c#,xml,wpf,multithreading,C#,Xml,Wpf,Multithreading,我尝试从WPF应用程序中的不同线程访问XMLNode的属性。但是,来自线程的第一次访问(不是XMLDocument的创建线程)会导致System.InvalidOperationException类型的异常 详情如下: 调用线程无法访问此对象,因为另一个 线程拥有它 这是我的密码 Monitor.Enter(lockAttribute); try { if (xmlInputNode.Attributes[sm_Attribute] != null) {
Monitor.Enter(lockAttribute);
try
{
if (xmlInputNode.Attributes[sm_Attribute] != null)
{
xmlInputNode.Attributes[sm_Attribute].InnerText = sm_AttributeValue.ToString();
}
}
finally
{
Monitor.Exit(lockAttribute);
}
我不理解这个例外。XMLDocument不是WPF控件,它将具有DependencyObject和关联的Dispatcher。请尝试以下操作:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
if (xmlInputNode.Attributes[sm_Attribute] != null)
{
xmlInputNode.Attributes[sm_Attribute].InnerText = sm_AttributeValue.ToString();
}
}));
在if(XML..)上设置断点。它可能在工作线程上。这个错误说,您应该使用主线程。