尝试访问USB设备时,RPC_E_CANTCALLOUT_ININPUTSYNCCALL [英] RPC_E_CANTCALLOUT_ININPUTSYNCCALL when trying to access USB device

查看:282
本文介绍了尝试访问USB设备时,RPC_E_CANTCALLOUT_ININPUTSYNCCALL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive");              
foreach (var queryObj in searcher.Get().Cast<ManagementObject>()) //Error points to this line

这个代码基本上是这样的,它运行在连接的设备列表中,看看我想要的是否连接。
如果在代码运行时设备已经连接的时候运行这个代码,那么这个代码是完美无缺的。
但是如果我用DBT_DEVICEARRIVAL(这是系统发送的事件,当某个设备连接,我抓住它与

Basically what this code does is, it runs through a list of connected devices and looks if the one i want is connected. If I run this code while the device is already connected at the time when the code is ran, then it works flawlessly. But if I trigger this code with DBT_DEVICEARRIVAL (which is event the system sends when some device is connected and i catch it with

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){if(..DBT_DEVICEARRIVAL..) new ScanDevices(); /*Here lies the code from above (in the class)*/}

)我得到这个错误:

由于应用程序正在调度输入同步调用,所以无法进行呼出。 (HRESULT的异常:0x8001010D(RPC_E_CANTCALLOUT_ININPUTSYNCCALL))。

An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)).

如果我将thread.sleep(5000)放在上面的代码之上,那么在执行之前等待5秒钟,那么代码工作。所以冲突必须在某个地方,其他的东西首先尝试访问该设备,并将其全部用于自己。

If I put thread.sleep(5000) on top of the code above, so it waits 5 seconds before executing, then the code works. So the conflict must be somewhere, where other things try to access that device first and hog it all for themselves.

我搜索互联网,发现建议,如发送自定义postmessage我自己来触发代码,但我对如何做到这一点没有什么意见,甚至如何解决这个问题。

I searched the internet and found suggestions like sending custom postmessage to myself to trigger the code, but I have little idea on how to do that, or even how would that solved the problem.

这里最好的解决方案是什么? p>

What is the best solution here?

推荐答案

将代码换成新线程:

Thread thread = new Thread(() =>
{
    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
    foreach (ManagementObject currentObject in theSearcher.Get())
    {
        Debug.WriteLine("Device present: " + currentObject);          
        ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
        serial = theSerialNumberObjectQuery["SerialNumber"].ToString();
    }
});
thread.Start();
thread.Join(); //wait for the thread to finish

这篇关于尝试访问USB设备时,RPC_E_CANTCALLOUT_ININPUTSYNCCALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆