C# UIACOMWrapper:删除AutomationEventHandler时出现InvalidOperationException
我使用下一个代码等待窗口打开:C# UIACOMWrapper:删除AutomationEventHandler时出现InvalidOperationException,c#,ui-automation,microsoft-ui-automation,C#,Ui Automation,Microsoft Ui Automation,我使用下一个代码等待窗口打开: Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, (sender, e) => { var element = sender as AutomationElement; if (element.Cur
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children,
(sender, e) =>
{
var element = sender as AutomationElement;
if (element.Current.Name != rolesFormTitle)
return;
Automation.RemoveAllEventHandlers();
SelectRoleOpenMainForm();
});
在添加对UIAComWrapper v1.1.0.14的引用(使用Nuget)之后,我开始得到这个异常(UIAComWrapper的源代码):
内部异常为空。
Factory.RemoveAllEventHandlers()也有同样的例外情况
我是否应该以某种方式准备某个对象的状态
更新1:
堆栈跟踪:
at UIAutomationClient.CUIAutomation8Class.RemoveAutomationEventHandler(Int32 eventId, IUIAutomationElement element, IUIAutomationEventHandler handler)
at System.Windows.Automation.Automation.RemoveAutomationEventHandler(AutomationEvent eventId, AutomationElement element, AutomationEventHandler eventHandler) in Automation.cs: line 239
Automation.cs()的源代码:
更新2:
源代码:
private void Form1_Load(object send, EventArgs ev)
{
Process.Start("calc");
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, (sender, e) =>
{
var element = sender as AutomationElement;
if (!element.Current.Name.StartsWith("Calculator"))
return;
Automation.RemoveAllEventHandlers();
MessageBox.Show("BINGO!");
});
}
我从来没有看到过“宾果!”。它依赖于自动化
更新3:
仍然挂起:
private void Form1_Load(object send, EventArgs ev)
{
Process.Start("calc");
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, (sender, e) =>
{
var element = sender as AutomationElement;
if (!element.Current.Name.StartsWith("Calculator"))
return;
this.Invoke(new Action(() => RemoveTry()));
});
}
private void RemoveTry()
{
Automation.RemoveAllEventHandlers(); // reachable
MessageBox.Show("BINGO!"); // unreachable
}
线程窗口:
找到的解决方案:
请参阅找到的解决方案的注释。很难说没有完整的复制代码和完整的堆栈跟踪直接绑定到此方法:这可能是线程问题。也许你也可以根据你的上下文忽略这个错误我在Windows 10上,不能用你的代码复制,但是,就像我说的,文档说你不应该使用不同的线程来添加或删除句柄,这就是你要做的,因为删除发生在另一个线程中,而不是添加。正如Simon Mourier所说,并且已经说过,您应该在同一线程上添加和删除事件处理程序,而不是从事件处理程序中添加和删除。尝试将删除代码传递给添加事件处理程序的同一线程。这可能会解决问题。这闻起来很像线程死锁。使用“调试>窗口>线程”窗口查看事件处理程序在哪个线程上运行。还要看看主UI线程在做什么。如果事件处理程序运行在同一UI线程上,请确保程序的Main()入口点仍然具有[StatThread]属性。
private void Form1_Load(object send, EventArgs ev)
{
Process.Start("calc");
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children, (sender, e) =>
{
var element = sender as AutomationElement;
if (!element.Current.Name.StartsWith("Calculator"))
return;
this.Invoke(new Action(() => RemoveTry()));
});
}
private void RemoveTry()
{
Automation.RemoveAllEventHandlers(); // reachable
MessageBox.Show("BINGO!"); // unreachable
}