// 启动观察线程 public static void StartWatch(delegate_changed callback, CancellationToken token) { List <USBDeviceInfo> Infos = new List <USBDeviceInfo>(); Infos = GetUSBDevices(); Task.Run(() => { while (token.IsCancellationRequested == false) { Task.Delay(TimeSpan.FromSeconds(2)).Wait(token); var result = GetUSBDevices(); // 和 Infos 比较 Compare(Infos, result, out int add_count, out int remove_count); Infos = result; if (add_count != 0 || remove_count != 0) { callback?.Invoke(add_count, remove_count); } } }); }
// 启动观察线程 public static void StartWatch(delegate_changed callback, CancellationToken token) { List <USBDeviceInfo> Infos = new List <USBDeviceInfo>(); Infos = GetUSBDevices(); #if SERIAL Infos.AddRange(GetSerialDevices()); #endif Task.Run(() => { try { while (token.IsCancellationRequested == false) { Task.Delay(TimeSpan.FromSeconds(2)).Wait(token); if (token.IsCancellationRequested) { break; } var result = GetUSBDevices(); if (token.IsCancellationRequested) { break; } #if SERIAL result.AddRange(GetSerialDevices()); #endif // 和 Infos 比较 Compare(Infos, result, out int add_count, out int remove_count); Infos = result; if (add_count != 0 || remove_count != 0) { callback?.Invoke(add_count, remove_count); } } } catch { } }); }