闪开我要上天了 2021-12-17 16:33 采纳率: 66.7%
浏览 34
已结题

监听USB设备插拔动态

listbox1添加设备串口时出现多次触发通知,该怎么处理?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Management;
using System.Threading;
using System.Diagnostics;
using System.Timers;


namespace Port_display
{
    public partial class Frm_Main : Form
   {
        public Frm_Main()
        {
            InitializeComponent();

        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            //Control.CheckForIllegalCrossThreadCalls = false;
            this.BeginInvoke((EventHandler)(delegate { listBox1.Items.AddRange(SerialProtFindHelper.GetSerialPort()); }));//跨线程事件
        }

        private const int WM_DEVICECHANGE = 0x219; //设备改变
        private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
        private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_DEVICECHANGE://设备改变事件
                    switch ((int)m.WParam)
                    {
                        case DBT_DEVICEARRIVAL:
                            
                            this.BeginInvoke((EventHandler)(delegate { listBox1.Items.AddRange(SerialProtFindHelper.GetSerialPort()); }));//跨线程事件
                            break;
                        case DBT_DEVICEREMOVECOMPLETE:
                            //MessageBox.Show("USB串口拔出");
                            listBox1.Items.Clear();
                            break;
                    }
                    break;
            }
        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月25日
    • 创建了问题 12月17日

    悬赏问题

    • ¥20 思科:Router c3600 MN-4E插槽
    • ¥15 16进制修改视频的元数据
    • ¥15 岛津txt格式文件转nirs格式
    • ¥15 石墨烯磁表面等离子体
    • ¥15 angular 项目无法启动
    • ¥15 安装wampserver,图标绿色,但是无法进入软件
    • ¥15 C++ MFC 标准库 加密解密解惑
    • ¥15 两条数据合并成一条数据
    • ¥15 Ubuntu虚拟机设置
    • ¥15 comsol三维模型中磁场为什么没有“速度(洛伦兹项)”这一选项