WMI获取USB设备信息报错 : 应用程序调用一个已为另一线程整理的接口。 (异常来自 HRESULT:0x8001010E (RPC_E_WRONG_THREAD))。

出错代码

 using (var managementClass = new ManagementClass("Win32_DiskDrive"))
                {
                        var devices = managementClass.GetInstances();
                        foreach (var o in devices)
                        {
                            try
                            {
                                var mo = (ManagementObject) o;
                                var device = mo["Caption"].ToString();
                                disks += mo["Caption"] + ";";
                            }
                            catch (Exception e)
                            {
                                _log.WriteLog(LogType.Error, $@"GetDisk:{e}");
                            }
                        }
                }      

  以上代码会抛出异常“GetDisk:System.InvalidCastException: 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“System.Management.IWbemServices”。此操作失败的原因是对 IID 为“{9556DC99-828C-11CF-A37E-00AA003240C7}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 应用程序调用一个已为另一线程整理的接口。 (异常来自 HRESULT:0x8001010E (RPC_E_WRONG_THREAD))。”

解决方法:

将获取设备信息在新的线程中运行

 using (var managementClass = new ManagementClass("Win32_DiskDrive"))
                {
                    var t = new Thread(() =>
                    {
                        var devices = managementClass.GetInstances();
                        foreach (var o in devices)
                        {
                            try
                            {
                                var mo = (ManagementObject) o;
                                var device = mo["Caption"].ToString();
                                disks += mo["Caption"] + ";";
                            }
                            catch (Exception e)
                            {
                                _log.WriteLog(LogType.Error, $@"GetDisk:{e}");
                            }
                        }
                    });
                    t.Start();
                    t.Join();
                }

  

 

posted @ 2020-03-20 14:14  Jazz_Law  阅读(2399)  评论(0编辑  收藏  举报
#RecentCommentsBlock li { margin: 0; width: 275px; } #RecentCommentsBlock li.recent_comment_body { border-radius: 0; margin: 0; } #RecentCommentsBlock li.recent_comment_title { border-radius: 5px 5px 0 0; margin: 3px 0 0; } #RecentCommentsBlock li.recent_comment_author { border-radius: 0 0 5px 5px; margin: 0; } .desc_img{ width:75px; max-width:75px; } #blog-calendar{ background:white; } /* comment */ div.commentform{ margin-bottom:100px; } #commentform_title { background: url("http://static.cnblogs.com/images/icon_addcomment.gif") no-repeat scroll 0 2px; color: #0078d8; font-size:14px; } div.commentform p{ margin-bottom:10px; } .comment_btn { height: 35px; width: 90px; background: none repeat scroll 0 0 #0078d8; border: 0 none; border-radius: 5px; color: white; cursor:pointer; } .comment_btn:hover{ background:#317ef3; } #commentbox_opt,#commentbox_opt + p { text-align:center; } #tbCommentBody{ width:100%; resize:none; } #tbCommentAuthor,#tbCommentBody{ border:1px solid #0078d8; } #tbCommentBody:hover{ border:1px solid #fca021; } #comments > h3 { background: none repeat scroll 0 0 #0078d8; border-radius: 3px; color: white; padding: 8px; border:0 none; font-size:14px; } #comments{ font-size: 13px; } #comments h4{ margin-top:10px; } #comments h4 span { color: #6c6351; font-size: 12px; } .comment_actions { border-bottom: 1px dashed #0078d8; display: block; padding-bottom: 10px; } .blog_comment_body { color: #111; font-size: 13px; margin-bottom: 10px; margin-top: 10px; } #comment_nav { font-size: 14px; margin-top: 10px; text-align: right; }