Remove ui thread from unplug routine

Hotplug routine was starting before removal routine
was being started. No blocking was happening anyway
This commit is contained in:
Travis Nickles 2018-08-20 04:35:41 -05:00
parent ac9e2411ac
commit 6f5b17ac37
3 changed files with 15 additions and 38 deletions

View File

@ -381,6 +381,7 @@ namespace DS4Windows
return unplugResult; return unplugResult;
} }
private SynchronizationContext uiContext = null;
public bool Start(object tempui, bool showlog = true) public bool Start(object tempui, bool showlog = true)
{ {
if (x360Bus.Open() && x360Bus.Start()) if (x360Bus.Open() && x360Bus.Start())
@ -391,6 +392,7 @@ namespace DS4Windows
LogDebug("Connection to Scp Virtual Bus established"); LogDebug("Connection to Scp Virtual Bus established");
DS4Devices.isExclusiveMode = getUseExclusiveMode(); DS4Devices.isExclusiveMode = getUseExclusiveMode();
uiContext = tempui as SynchronizationContext;
if (showlog) if (showlog)
{ {
LogDebug(Properties.Resources.SearchingController); LogDebug(Properties.Resources.SearchingController);
@ -424,7 +426,6 @@ namespace DS4Windows
task.Start(); task.Start();
DS4Controllers[i] = device; DS4Controllers[i] = device;
device.setUiContext(tempui as SynchronizationContext);
device.Removal += this.On_DS4Removal; device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal; device.Removal += DS4Devices.On_Removal;
device.SyncChange += this.On_SyncChange; device.SyncChange += this.On_SyncChange;
@ -618,7 +619,7 @@ namespace DS4Windows
return true; return true;
} }
public bool HotPlug(SynchronizationContext uiContext) public bool HotPlug()
{ {
if (running) if (running)
{ {
@ -655,7 +656,6 @@ namespace DS4Windows
Task task = new Task(() => { Thread.Sleep(5); WarnExclusiveModeFailure(device); }); Task task = new Task(() => { Thread.Sleep(5); WarnExclusiveModeFailure(device); });
task.Start(); task.Start();
DS4Controllers[Index] = device; DS4Controllers[Index] = device;
device.setUiContext(uiContext);
device.Removal += this.On_DS4Removal; device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal; device.Removal += DS4Devices.On_Removal;
device.SyncChange += this.On_SyncChange; device.SyncChange += this.On_SyncChange;
@ -1072,7 +1072,7 @@ namespace DS4Windows
string devError = tempStrings[ind] = device.error; string devError = tempStrings[ind] = device.error;
if (!string.IsNullOrEmpty(devError)) if (!string.IsNullOrEmpty(devError))
{ {
device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) uiContext?.Post(new SendOrPostCallback(delegate (object state)
{ {
LogDebug(devError); LogDebug(devError);
}), null); }), null);
@ -1084,7 +1084,7 @@ namespace DS4Windows
if (!lag[ind] && device.Latency >= flashWhenLateAt) if (!lag[ind] && device.Latency >= flashWhenLateAt)
{ {
lag[ind] = true; lag[ind] = true;
device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) uiContext?.Post(new SendOrPostCallback(delegate (object state)
{ {
LagFlashWarning(ind, true); LagFlashWarning(ind, true);
}), null); }), null);
@ -1092,7 +1092,7 @@ namespace DS4Windows
else if (lag[ind] && device.Latency < flashWhenLateAt) else if (lag[ind] && device.Latency < flashWhenLateAt)
{ {
lag[ind] = false; lag[ind] = false;
device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) uiContext?.Post(new SendOrPostCallback(delegate (object state)
{ {
LagFlashWarning(ind, false); LagFlashWarning(ind, false);
}), null); }), null);
@ -1115,7 +1115,7 @@ namespace DS4Windows
if (device.firstReport && device.IsAlive()) if (device.firstReport && device.IsAlive())
{ {
device.firstReport = false; device.firstReport = false;
device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) uiContext?.Post(new SendOrPostCallback(delegate (object state)
{ {
OnDeviceStatusChanged(this, ind); OnDeviceStatusChanged(this, ind);
}), null); }), null);
@ -1124,7 +1124,7 @@ namespace DS4Windows
{ {
byte tempBattery = currentBattery[ind] = cState.Battery; byte tempBattery = currentBattery[ind] = cState.Battery;
bool tempCharging = charging[ind] = device.isCharging(); 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); OnBatteryStatusChange(this, ind, tempBattery, tempCharging);
}), null); }), null);

View File

@ -1010,11 +1010,10 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question
hotplugCounter++; hotplugCounter++;
} }
var uiContext = SynchronizationContext.Current;
if (!inHotPlug) if (!inHotPlug)
{ {
inHotPlug = true; 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); base.WndProc(ref m);
} }
private void InnerHotplug2(SynchronizationContext uiContext) private void InnerHotplug2()
{ {
inHotPlug = true; inHotPlug = true;
@ -1040,7 +1039,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question
while (loopHotplug == true) while (loopHotplug == true)
{ {
Program.rootHub.HotPlug(uiContext); Program.rootHub.HotPlug();
//TaskRunner.Run(() => { Program.rootHub.HotPlug(uiContext); }); //TaskRunner.Run(() => { Program.rootHub.HotPlug(uiContext); });
lock (hotplugCounterLock) lock (hotplugCounterLock)
{ {

View File

@ -392,16 +392,6 @@ namespace DS4Windows
return result; return result;
} }
private SynchronizationContext uiContext = null;
public SynchronizationContext getUiContext()
{
return uiContext;
}
public void setUiContext(SynchronizationContext uiContext)
{
this.uiContext = uiContext;
}
private Queue<Action> eventQueue = new Queue<Action>(); private Queue<Action> eventQueue = new Queue<Action>();
private object eventQueueLock = new object(); private object eventQueueLock = new object();
@ -808,10 +798,7 @@ namespace DS4Windows
sendOutputReport(true, true); // Kick Windows into noticing the disconnection. sendOutputReport(true, true); // Kick Windows into noticing the disconnection.
StopOutputUpdate(); StopOutputUpdate();
isDisconnecting = true; isDisconnecting = true;
uiContext.Send(new SendOrPostCallback(delegate (object state4) Removal?.Invoke(this, EventArgs.Empty);
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
timeoutExecuted = true; timeoutExecuted = true;
return; return;
@ -838,10 +825,7 @@ namespace DS4Windows
StopOutputUpdate(); StopOutputUpdate();
isDisconnecting = true; isDisconnecting = true;
uiContext.Send(new SendOrPostCallback(delegate (object state4) Removal?.Invoke(this, EventArgs.Empty);
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
timeoutExecuted = true; timeoutExecuted = true;
return; return;
@ -1292,10 +1276,7 @@ namespace DS4Windows
if (callRemoval) if (callRemoval)
{ {
uiContext.Send(new SendOrPostCallback(delegate (object state) Removal?.Invoke(this, EventArgs.Empty);
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
//System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); });
} }
@ -1327,10 +1308,7 @@ namespace DS4Windows
{ {
isDisconnecting = true; isDisconnecting = true;
uiContext.Send(new SendOrPostCallback(delegate (object state4) Removal?.Invoke(this, EventArgs.Empty);
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
//System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); });
//Removal?.Invoke(this, EventArgs.Empty); //Removal?.Invoke(this, EventArgs.Empty);