我有一个代表 USB 设备终端的表单,它一直给我一些错误。在调试了半天未知来源的奇怪错误后,我不知何故发现终端在实例化但未显示时不起作用。当我更改代码并添加 usbTerminal.Show(); 时,它可以正常工作。
USBTerminal usbTouchTerminal;
public MainForm()
{
InitializeComponent();
USBSettings usbTouchSettings = new USBSettings();
usbTouchTerminal = new USBTerminal(usbTouchSettings); //Create Terminal with settings
usbTouchTerminal.StartUSB();
usbTouchTerminal.Show(); //works ONLY when show is here
}
这怎么可能,为什么?我进行了大量搜索,但我的代码都不依赖于终端或主窗体上的 .Visible 属性?
我完全困惑为什么某些表格如果没有显示就不起作用。MSDN 或谷歌也不是真正的帮助。我确信它在实例化但未显示时会正常运行。
PS。我添加了
usbTerminal.Show();
usbTerminal.Hide();
并且终端正常运行。
感谢您的任何帮助!
编辑:
我还应该注意这个 usbTerminal 使用 WndProc 覆盖。我不是这方面的专家,但我觉得这可能与它有关。
我应该注意到这是 LibUSBdotnet
public class USBSettings
{
/// <summary>
/// This is the Vender ID Number. (0x0B6A)
/// </summary>
public ushort VID { get; set; }
/// <summary>
/// This is the Product ID Number. (0x5346)
/// </summary>
public ushort PID { get; set; }
/// <summary>
/// This is the optional Serial Name. ("")
/// </summary>
public string SerialName { get; set; }
/// <summary>
/// This is the Reader USB Endpoint. (ReadEndpointID.Ep02)
/// </summary>
public ReadEndpointID ReaderEndpoint { get; set; }
/// <summary>
/// This is the Writer USB Endpoint. (WriteEndpointID.Ep01)
/// </summary>
public WriteEndpointID WriterEndpoint { get; set; }
/// <summary>
/// This is the Registry Key for USB settings. ("SOFTWARE\\DEFAULT\\USBPROPERTIES")
/// </summary>
public string SubKey { get; set; }
/// <summary>
/// This is the default read buffer size for the USB Device.
/// </summary>
public int ReadBufferSize { get; set; }
/// <summary>
/// This constructor houses default values for all properties.
/// </summary>
public USBSettings()
{
VID = 0x0B6A;
PID = 0x5346;
SerialName = "";
ReaderEndpoint = ReadEndpointID.Ep02;
WriterEndpoint = WriteEndpointID.Ep01;
SubKey = "SOFTWARE\\DEFAULT\\USBPROPERTIES";
ReadBufferSize = 100;
}
}