diff --git a/DS4Windows/DS4Control/ControlSerivce.cs b/DS4Windows/DS4Control/ControlSerivce.cs index e868c39..2621435 100644 --- a/DS4Windows/DS4Control/ControlSerivce.cs +++ b/DS4Windows/DS4Control/ControlSerivce.cs @@ -248,6 +248,21 @@ namespace DS4Windows { if (running) { + // Do first run check for Quick Charge checks. Needed so old device will + // be removed before performing another controller scan + for (int i = 0, devlen = DS4Controllers.Length; i < devlen; i++) + { + DS4Device device = DS4Controllers[i]; + if (device != null) + { + if (getQuickCharge() && device.getConnectionType() == ConnectionType.BT && + device.isCharging()) + { + device.DisconnectBT(); + } + } + } + DS4Devices.findControllers(); IEnumerable devices = DS4Devices.getDS4Controllers(); //foreach (DS4Device device in devices) @@ -258,13 +273,6 @@ namespace DS4Windows if (device.isDisconnectingStatus()) continue; - if (getQuickCharge() && device?.getConnectionType() == ConnectionType.BT && - (bool)device?.isCharging()) - { - device.DisconnectBT(); - continue; - } - if (((Func)delegate { for (Int32 Index = 0, arlength = DS4Controllers.Length; Index < arlength; Index++) diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index a133ec4..234d963 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -934,9 +934,11 @@ namespace DS4Windows lbLastMessage.Text = string.Empty; } + //delegate void OldHotPlugDelegate(); + bool skipHotplug = false; protected override void WndProc(ref Message m) { - if (runHotPlug) + if (runHotPlug && !skipHotplug) { try { @@ -946,7 +948,14 @@ namespace DS4Windows lock (this) { - Program.rootHub.HotPlug(); + // Execute hotplug routine after a delay in time. Set flag so extra calls + // to WndProc will be ignored + skipHotplug = true; + System.Threading.Tasks.Task.Delay(50).ContinueWith((t) => InnerHotplug()); + //OldHotPlugDelegate d = new OldHotPlugDelegate(InnerHotplug); + //this.BeginInvoke(d); + //skipHotplug = false; + //Program.rootHub.HotPlug(); } } } @@ -961,6 +970,19 @@ namespace DS4Windows catch { } } + delegate bool HotPlugDelegate(); + protected void InnerHotplug() + { + lock (this) + { + // Reset flag and execute hotplug routine in main thread + skipHotplug = false; + HotPlugDelegate d = new HotPlugDelegate(Program.rootHub.HotPlug); + this.BeginInvoke(d); + //Program.rootHub.HotPlug(); + } + } + protected void BatteryStatusUpdate(object sender, BatteryReportArgs args) { if (this.InvokeRequired)