托管调试助手“DisconnectedContext”在“[ProjectDirectory]\[ProjectName].exe”中检测到问题。
其他信息: 针对此 RuntimeCallableWrapper 向 COM 上下文 0x8306e8 的转换失败,错误如下: 因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫。 (异常来自 HRESULT:0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))。原因通常是创建此 RuntimeCallableWrapper 的 COM 上下文 0x8306e8 已断开连接,或者该上下文正忙于执行其他操作,无法处理该上下文转换。将不会有代理服务于该 COM 组件上的请求,调用将直接转向该 COM 组件。这可能会导致损坏或数据丢失。要避免此问题,请确保在应用程序全部完成 RuntimeCallableWrapper (表示其内部的 COM 组件)之前,所有 COM 上下文/单元/线程都保持活动状态并可用于上下文转换。
问题位置 :var hardinfos = searcher.Get();
namespace SMQH
{
public class hosComputerSystemHardware
{
public bool Main()
{
try
{
string[] comstr = GetSerialPortList();
if (comstr == null)
{
System.Diagnostics.Debug.WriteLine("not find serial port!");
return false;
}
for (int i = 0; i < comstr.Length; i++)
{
System.Diagnostics.Debug.WriteLine(comstr[i]);
}
}
catch (System.IO.IOException exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
return false;
}
catch (Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
return false;
}
return false;
}
///
/// 获取硬件设备信息
///
/// 设备类型
/// 硬件名称
///
private static string[] MulGetHardwareInfo(ComputerSystemHardwareClasses eType, Win32_PnPEntitySection Keyvalue)
{
List strs = new List();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + eType))
{
// ManagementObjectCollection hardinfos = searcher.Get();
var hardinfos = searcher.Get();
foreach (var hardinfo in hardinfos)
{
if (hardinfo.Properties[Keyvalue.ToString()].Value != null)
{
if (hardinfo.Properties[Keyvalue.ToString()].Value.ToString().Contains("COM"))
{
strs.Add(hardinfo.Properties[Keyvalue.ToString()].Value.ToString());
}
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch (Exception exp)
{
System.Windows.Forms.MessageBox.Show(exp.Message, "异常");
return null;
}
}
///
/// 获取串口列表
///
///
private static string[] GetSerialPortList()
{
return MulGetHardwareInfo(ComputerSystemHardwareClasses.Win32_PnPEntity, Win32_PnPEntitySection.Name);
}
}
}
创建线程来调用接口
namespace SMQH
{
public class hosComputerSystemHardware
{
///
/// 读值线程
///
private System.Threading.Thread threadReadValue = null;
///
/// 局部变量 用来存储返回结果
///
private string[] PortNameArray = null;
///
/// 是否结束
///
public bool ReadOver = true;
public bool Main()
{
try
{
string[] comstr = GetSerialPortArray();
if (comstr == null)
{
System.Diagnostics.Debug.WriteLine("not find serial port!");
return false;
}
for (int i = 0; i < comstr.Length; i++)
{
System.Diagnostics.Debug.WriteLine(comstr[i]);
}
}
catch (System.IO.IOException exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
return false;
}
catch (Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
return false;
}
return false;
}
///
/// 获取串口列表线程
///
private void OnGetSerialPortList()
{
try
{
PortNameArray = GetSerialPortList();
}
catch (Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
ReadOver = true;
}
///
/// 通过线程获取串口列表
///
public string[] GetSerialPortArray()
{
PortNameArray = null;
try
{
threadReadValue = new System.Threading.Thread(OnGetSerialPortList);
threadReadValue.IsBackground = true;
ReadOver = false;
threadReadValue.Start();
while (ReadOver == false)
{
System.Threading.Thread.Sleep(200);
}
threadReadValue = null;
}
catch (Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
return PortNameArray;
}
///
/// 获取硬件设备信息
///
/// 设备类型
/// 硬件名称
///
private static string[] MulGetHardwareInfo(ComputerSystemHardwareClasses eType, Win32_PnPEntitySection Keyvalue)
{
List strs = new List();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + eType))
{
// ManagementObjectCollection hardinfos = searcher.Get();
var hardinfos = searcher.Get();
foreach (var hardinfo in hardinfos)
{
if (hardinfo.Properties[Keyvalue.ToString()].Value != null)
{
if (hardinfo.Properties[Keyvalue.ToString()].Value.ToString().Contains("COM"))
{
strs.Add(hardinfo.Properties[Keyvalue.ToString()].Value.ToString());
}
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch (Exception exp)
{
System.Windows.Forms.MessageBox.Show(exp.Message, "异常");
return null;
}
}
///
/// 获取串口列表
///
///
private static string[] GetSerialPortList()
{
return MulGetHardwareInfo(ComputerSystemHardwareClasses.Win32_PnPEntity, Win32_PnPEntitySection.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Data;
using System.ServiceProcess;
using NHChemi.BIOOIInfo;
using System.Text.RegularExpressions;
using System.Management;
using System.IO;
namespace SMQH
{
///
/// 电脑系统硬件类
///
public enum ComputerSystemHardwareClasses
{
Win32_1394Controller,
Win32_1394ControllerDevice,
Win32_Fan,
Win32_HeatPipe,
Win32_Refrigeration,
Win32_TemperatureProbe,
Win32_AssociatedProcessorMemory,
Win32_AutochkSetting,
Win32_BaseBoard,
Win32_Battery,
Win32_BIOS,
Win32_Bus,
Win32_CacheMemory,
Win32_CDROMDrive,
Win32_CIMLogicalDeviceCIMDataFile,
Win32_ComputerSystemProcessor,
Win32_CurrentProbe,
Win32_DesktopMonitor,
Win32_DeviceBus,
Win32_DeviceChangeEvent,
Win32_DeviceMemoryAddress,
Win32_DeviceSettings,
Win32_DiskDrive,
Win32_DiskDriveToDiskPartition,
Win32_DiskPartition,
Win32_DisplayControllerConfiguration,
Win32_DMAChannel,
Win32_DriverForDevice,
Win32_FloppyController,
Win32_FloppyDrive,
Win32_IDEController,
Win32_IDEControllerDevice,
Win32_InfraredDevice,
Win32_IRQResource,
Win32_Keyboard,
Win32_LogicalDisk,
Win32_LogicalDiskRootDirectory,
Win32_LogicalDiskToPartition,
Win32_LogicalProgramGroup,
Win32_LogicalProgramGroupDirectory,
Win32_LogicalProgramGroupItem,
Win32_LogicalProgramGroupItemDataFile,
Win32_MappedLogicalDisk,
Win32_MemoryArray,
Win32_MemoryArrayLocation,
Win32_MemoryDevice,
Win32_MemoryDeviceArray,
Win32_MemoryDeviceLocation,
Win32_MotherboardDevice,
Win32_NetworkAdapter,
Win32_NetworkAdapterConfiguration,
Win32_NetworkAdapterSetting,
Win32_NetworkClient,
Win32_NetworkConnection,
Win32_NetworkLoginProfile,
Win32_NetworkProtocol,
Win32_OnBoardDevice,
Win32_ParallelPort,
Win32_PCMCIAController,
Win32_PhysicalMemory,
Win32_PhysicalMemoryArray,
Win32_PhysicalMemoryLocation,
Win32_PnPAllocatedResource,
Win32_PnPDevice,
Win32_PnPDeviceProperty,
Win32_PnPDevicePropertyUint8,
Win32_PnPDevicePropertyUint16,
Win32_PnPDevicePropertyUint32,
Win32_PnPDevicePropertyUint64,
Win32_PnPDevicePropertySint8,
Win32_PnPDevicePropertySint16,
Win32_PnPDevicePropertySint32,
Win32_PnPDevicePropertySint64,
Win32_PnPDevicePropertyString,
Win32_PnPDevicePropertyBoolean,
Win32_PnPDevicePropertyReal32,
Win32_PnPDevicePropertyReal64,
Win32_PnPDevicePropertyDateTime,
Win32_PnPDevicePropertySecurityDescriptor,
Win32_PnPDevicePropertyBinary,
Win32_PnPDevicePropertyUint16Array,
Win32_PnPDevicePropertyUint32Array,
Win32_PnPDevicePropertyUint64Array,
Win32_PnPDevicePropertySint8Array,
Win32_PnPDevicePropertySint16Array,
Win32_PnPDevicePropertySint32Array,
Win32_PnPDevicePropertySint64Array,
Win32_PnPDevicePropertyStringArray,
Win32_PnPDevicePropertyBooleanArray,
Win32_PnPDevicePropertyReal32Array,
Win32_PnPDevicePropertyReal64Array,
Win32_PnPDevicePropertyDateTimeArray,
Win32_PnPDevicePropertySecurityDescriptorArray,
Win32_PnPEntity,
Win32_PointingDevice,
Win32_PortableBattery,
Win32_PortConnector,
Win32_PortResource,
Win32_POTSModem,
Win32_POTSModemToSerialPort,
Win32_Printer,
Win32_PrinterConfiguration,
Win32_PrinterController,
Win32_PrinterDriver,
Win32_PrinterDriverDll,
Win32_PrinterSetting,
Win32_PrinterShare,
Win32_PrintJob,
Win32_Processor,
Win32_SCSIController,
Win32_SCSIControllerDevice,
Win32_SerialPort,
Win32_SerialPortConfiguration,
Win32_SerialPortSetting,
Win32_SMBIOSMemory,
Win32_SoundDevice,
Win32_TapeDrive,
Win32_TCPIPPrinterPort,
Win32_USBController,
Win32_USBControllerDevice,
Win32_VideoController,
Win32_VideoSettings,
Win32_VoltageProbe,
}
///
/// 硬件信息字段
///
public enum Win32_PnPEntitySection
{
Availability, //uint16
Caption, //string
ClassGuid, //string
CompatibleID, //string
ConfigManagerErrorCode, //uint32
ConfigManagerUserConfig, //boolean
CreationClassName, //string
Description, //string
DeviceID, //string
ErrorCleared, //boolean
ErrorDescription, //string
HardwareID, //string
InstallDate, //datetime
LastErrorCode, //uint32
Manufacturer, //string
Name, //string
PNPClass, //string
PNPDeviceID, //string
PowerManagementCapabilities, //uint16
PowerManagementSupported, //boolean
Present, //boolean
Service, //string
Status, //string
StatusInfo, //uint16
SystemCreationClassName, //string
SystemName, //string
};
}