Exemplo n.º 1
0
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WmDevicechange)
            {
                //Debug.Print($"WmDevicechange {wParam} {lParam}");
                //Debug.Print($"  " + string.Join(", ", SerialPortList.GetBasicList().Select(x => x.Name)));

                var newPorts = SerialPortList.GetNames();

                if (newPorts.Count() != ports.Count)
                {
                    var added   = newPorts.Except(ports);
                    var removed = ports.Except(newPorts);

                    ports = newPorts.ToList();

                    foreach (var i in added)
                    {
                        Debug.Print("New port " + i);
                        Task.Run(() => Dispatcher.Invoke(() => ShowNewPortNotification(i)));
                    }

                    foreach (var i in removed)
                    {
                        Debug.Print("Removed port " + i);
                        Task.Run(() => Dispatcher.Invoke(() => ShowRemovedPortNotification(i)));
                    }
                }
            }


            return(IntPtr.Zero);
        }
Exemplo n.º 2
0
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            ports = SerialPortList.GetNames().ToList();

            var source = PresentationSource.FromVisual(this) as HwndSource;

            source.AddHook(WndProc);

            this.Visibility = Visibility.Hidden;
        }