【发布时间】:2016-05-11 06:01:05
【问题描述】:
我使用 MouseKeyboardActivityMonitor 为用户活动设置了一些限制,例如禁用鼠标。
www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
我的表单中有这些代码
public partial class MyForm:Form
{
KeyboardHookListener kl;
MouseHookListener ml;
MyForm:Form()
{
ml = new MouseHookListener( new GlobalHooker());
ml.Enabled=true;
}
private void MyForm_Load
{
ml.MouseDownExt += ml_MouseDownExt;
// And same thing for Click or ...
}
private void ml_MouseDownExt( object sender,MouseEventExtArgs e)
{
e.Handled= true;
// I have got hard disk serial number here
string sn = HardDisk.Serial;
}
}
以及HardDisk.Serial的代码
ManagementObjectSearcher s= new ManagementObjectSearcher(" SELECT *...");
foreach( var wmi in s.Get())
{
}
点击 MyForm 时出现错误。
当我构建我的解决方案并手动运行它时 我收到此错误
The application called an interface that was Marshalled for different thread
堆栈:
at system.management.MangementException.ThrowWithExtendedInfo( Exception e) at system.management.MangementObjectSearcher.Get() at HardDisk.Get_serial() at ml_MouseDownExt( object sender,MouseEventExtArgs e) at MouseKeyboardActivityMonitor.MouseHookListener.InvokeMouseEventHandlerExt(EventHandler'1 handler,MouseEventExtArgs e)
但是当我使用 Visual Studio 运行我的解决方案时,HardDisk.serial 会抛出异常 在 s.Get line ,我明白了
错误:
Managed debugging assistant ' DisconnectedContext' Has detected a problem in 'my app Name.exe' Transition into com context 0xa4206 for this runtime callable wrapper failed with following error : an outgoing call can't be made since the application is dispatching an input asynchronous call
很明显,两个错误来自 MangementObjectSearcher 类。我在 MyForm 的另一个位置获取序列号。错误只是在 ml_MouseDownExt 方法或已添加到 GlobalHooker 事件的其他方法中获取序列时发生。我见过 msdn。在 MangementObjectSearcher 的继承层次结构中,我看到 System.MarshalByRefObject
https://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher(v=vs.110).aspx
不知道是不是和这些错误有关
我应该如何避免这些错误?
【问题讨论】:
-
不清楚你做了什么来得到这个MDA,它肯定很糟糕。 sn-ps 和 Codeproject.com 项目都没有给出任何提示。返回 Program.cs 文件并验证 Main() 方法是否仍然具有 [STAThread] 属性。并且永远,永远 在工作线程上创建表单对象,除非您真的知道自己在做什么。几乎没有人这样做。
标签: c# winforms exception hook