尝试访问USB设备时,RPC

编程入门 行业动态 更新时间:2024-10-11 19:20:31
本文介绍了尝试访问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

本文发布于:2023-11-27 15:04:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设备   USB   RPC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!