private void SerialUpdate(object sender, EventArgs e) { Thread threadDeveiceChanged = new Thread(() => { string[] portnames = MainWindowMonitor.MulGetHardwareInfo(MainWindowMonitor.HardwareEnum.Win32_PnPEntity, "Name"); Dispatcher.Invoke(new Action(() => { if (turnOnButton.Content.Equals("关闭串口")) { bool flag = true; //查找所有存在的串口 for (int i = 0; i < portnames.Length; i++) { if (portnames[i].Contains(serial.PortName)) { flag = false;//不是本串口被拔 } } if (flag == true)//所有存在的串口中找不到已经打开的串口 { Close_Port(); AddValuablePortName(portnames); statusTextBlock.Text = "设备被拔出!"; } } else { AddValuablePortName(portnames); } })); }); threadDeveiceChanged.Start(); }
//搜索串口名 private void search_serial() { Thread threadDeveiceChanged = new Thread(() => { AddValuablePortName(MainWindowMonitor.MulGetHardwareInfo(MainWindowMonitor.HardwareEnum.Win32_PnPEntity, "Name")); }); threadDeveiceChanged.Start(); }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (MessageBox.Show("是否要关闭?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { e.Cancel = false; } else { e.Cancel = true; } MainWindowMonitor.WindowClosedEventFunction(); }
private IntPtr DeveiceChanged(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == MainWindowMonitor.WM_DEVICECHANGE) { switch (wParam.ToInt32()) { case MainWindowMonitor.DBT_DEVICEARRIVAL: //设备插入 MainWindowMonitor.DeviceChangedFunction(); break; case MainWindowMonitor.DBT_DEVICEREMOVECOMPLETE: //设备卸载 MainWindowMonitor.DeviceChangedFunction(); break; default: break; } } return(IntPtr.Zero); }