From 6f5b17ac374ff81b216acf358c56aaed79ee7da3 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Mon, 20 Aug 2018 04:35:41 -0500 Subject: [PATCH] Remove ui thread from unplug routine Hotplug routine was starting before removal routine was being started. No blocking was happening anyway --- DS4Windows/DS4Control/ControlService.cs | 16 ++++++------- DS4Windows/DS4Forms/DS4Form.cs | 7 +++--- DS4Windows/DS4Library/DS4Device.cs | 30 ++++--------------------- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index 75cf40b..0994918 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -381,6 +381,7 @@ namespace DS4Windows return unplugResult; } + private SynchronizationContext uiContext = null; public bool Start(object tempui, bool showlog = true) { if (x360Bus.Open() && x360Bus.Start()) @@ -391,6 +392,7 @@ namespace DS4Windows LogDebug("Connection to Scp Virtual Bus established"); DS4Devices.isExclusiveMode = getUseExclusiveMode(); + uiContext = tempui as SynchronizationContext; if (showlog) { LogDebug(Properties.Resources.SearchingController); @@ -424,7 +426,6 @@ namespace DS4Windows task.Start(); DS4Controllers[i] = device; - device.setUiContext(tempui as SynchronizationContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += this.On_SyncChange; @@ -618,7 +619,7 @@ namespace DS4Windows return true; } - public bool HotPlug(SynchronizationContext uiContext) + public bool HotPlug() { if (running) { @@ -655,7 +656,6 @@ namespace DS4Windows Task task = new Task(() => { Thread.Sleep(5); WarnExclusiveModeFailure(device); }); task.Start(); DS4Controllers[Index] = device; - device.setUiContext(uiContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += this.On_SyncChange; @@ -1072,7 +1072,7 @@ namespace DS4Windows string devError = tempStrings[ind] = device.error; if (!string.IsNullOrEmpty(devError)) { - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LogDebug(devError); }), null); @@ -1084,7 +1084,7 @@ namespace DS4Windows if (!lag[ind] && device.Latency >= flashWhenLateAt) { lag[ind] = true; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LagFlashWarning(ind, true); }), null); @@ -1092,7 +1092,7 @@ namespace DS4Windows else if (lag[ind] && device.Latency < flashWhenLateAt) { lag[ind] = false; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LagFlashWarning(ind, false); }), null); @@ -1115,7 +1115,7 @@ namespace DS4Windows if (device.firstReport && device.IsAlive()) { device.firstReport = false; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { OnDeviceStatusChanged(this, ind); }), null); @@ -1124,7 +1124,7 @@ namespace DS4Windows { byte tempBattery = currentBattery[ind] = cState.Battery; bool tempCharging = charging[ind] = device.isCharging(); - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { OnBatteryStatusChange(this, ind, tempBattery, tempCharging); }), null); diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index fa9d9c2..3b4348e 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -1010,11 +1010,10 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question hotplugCounter++; } - var uiContext = SynchronizationContext.Current; if (!inHotPlug) { inHotPlug = true; - TaskRunner.Run(() => { Thread.Sleep(1500); InnerHotplug2(uiContext); }); + TaskRunner.Run(() => { Thread.Sleep(1500); InnerHotplug2(); }); } } } @@ -1028,7 +1027,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question base.WndProc(ref m); } - private void InnerHotplug2(SynchronizationContext uiContext) + private void InnerHotplug2() { inHotPlug = true; @@ -1040,7 +1039,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question while (loopHotplug == true) { - Program.rootHub.HotPlug(uiContext); + Program.rootHub.HotPlug(); //TaskRunner.Run(() => { Program.rootHub.HotPlug(uiContext); }); lock (hotplugCounterLock) { diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index db239f7..560894b 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -392,16 +392,6 @@ namespace DS4Windows return result; } - private SynchronizationContext uiContext = null; - public SynchronizationContext getUiContext() - { - return uiContext; - } - public void setUiContext(SynchronizationContext uiContext) - { - this.uiContext = uiContext; - } - private Queue eventQueue = new Queue(); private object eventQueueLock = new object(); @@ -808,10 +798,7 @@ namespace DS4Windows sendOutputReport(true, true); // Kick Windows into noticing the disconnection. StopOutputUpdate(); isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); timeoutExecuted = true; return; @@ -838,10 +825,7 @@ namespace DS4Windows StopOutputUpdate(); isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); timeoutExecuted = true; return; @@ -1292,10 +1276,7 @@ namespace DS4Windows if (callRemoval) { - uiContext.Send(new SendOrPostCallback(delegate (object state) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); } @@ -1327,10 +1308,7 @@ namespace DS4Windows { isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); //Removal?.Invoke(this, EventArgs.Empty);