【问题标题】:The application called an interface that was Marshalled for different thread error when using MangementObjectSearcher应用程序调用了一个接口,该接口在使用 MangementObjectSearcher 时针对不同的线程错误进行了编组
【发布时间】: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


【解决方案1】:

您的钩子回调没有在正确的线程上引发。这只是第一个问题。将其包装在 BeginInvoke 中,一切都会好起来的:

private void ml_MouseDownExt( object sender,MouseEventExtArgs  e)
{
      e.Handled= true;

      var wrongThread = new Action(()=>
      {
          // I have got hard disk serial number here
          string sn = HardDisk.Serial;
          //put anything else you were planning on doing with sn here
      }
      BeginInvoke(wrongThread, null);
}

第二个问题是您试图与全局挂钩处理程序中的 COM 对象进行交互。 BeginInvoke 应该可以通过延迟几微秒来很好地解决这个问题。

不要忘记确保在该全局挂钩上调用 Dispose。除非您喜欢经常重启,否则关闭应用是不够的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多