From 6190ddff9b40317a1fd83ccda7ad78aa31a9043e Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Sat, 26 Aug 2017 04:25:42 -0500 Subject: [PATCH] Change thread affinity of DS4Device objects Drastically improves performance. Might be able to remove the need for the separate input thread later. --- DS4Windows/DS4Control/ControlService.cs | 4 +- DS4Windows/DS4Forms/DS4Form.cs | 81 ++++++++++++++++--------- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index 469fc97..5789662 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -107,7 +107,7 @@ namespace DS4Windows } } - public bool Start(bool showlog = true) + public bool Start(object tempui, bool showlog = true) { if (x360Bus.Open() && x360Bus.Start()) { @@ -141,7 +141,7 @@ namespace DS4Windows task.Start(); DS4Controllers[i] = device; - device.setUiContext(SynchronizationContext.Current); + device.setUiContext(tempui as SynchronizationContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += this.On_SyncChange; diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index 217afa4..cc6f0a9 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -16,6 +16,7 @@ using Microsoft.Win32.TaskScheduler; using System.Security.Principal; using System.Threading; using System.Drawing.Drawing2D; +using TaskRunner = System.Threading.Tasks.Task; using static DS4Windows.Global; namespace DS4Windows @@ -416,7 +417,7 @@ namespace DS4Windows } } - System.Threading.Tasks.Task.Run(() => { UpdateTheUpdater(); }); + TaskRunner.Run(() => { UpdateTheUpdater(); }); this.StartWindowsCheckBox.CheckedChanged += new EventHandler(this.StartWindowsCheckBox_CheckedChanged); new ToolTip().SetToolTip(StartWindowsCheckBox, Properties.Resources.RunAtStartup); @@ -465,14 +466,11 @@ namespace DS4Windows private void blankControllerTab() { - bool nocontrollers = true; - for (Int32 Index = 0, PadsLen = Pads.Length; Index < PadsLen; Index++) + for (int Index = 0, PadsLen = Pads.Length; + Index < PadsLen; Index++) { - // Make sure a controller exists if (Index < ControlService.DS4_CONTROLLER_COUNT) { - Pads[Index].Text = ""; - statPB[Index].Visible = false; toolTip1.SetToolTip(statPB[Index], ""); Batteries[Index].Text = Properties.Resources.NA; Pads[Index].Text = Properties.Resources.Disconnected; @@ -480,8 +478,8 @@ namespace DS4Windows } } - lbNoControllers.Visible = nocontrollers; - tLPControllers.Visible = !nocontrollers; + lbNoControllers.Visible = true; + tLPControllers.Visible = false; } private void UpdateTheUpdater() @@ -946,32 +944,59 @@ namespace DS4Windows btnStartStop_Clicked(); } + private void serviceStartup(bool log) + { + var uiContext = SynchronizationContext.Current; + TaskRunner.Run(() => + { + Program.rootHub.Start(uiContext, log); + this.Invoke((System.Action)(() => { serviceStartupFinish(); })); + }); + } + + private void serviceStartupFinish() + { + if (SwipeProfiles && !hotkeysTimer.Enabled) + { + hotkeysTimer.Start(); + } + + if (programpaths.Count > 0 && !autoProfilesTimer.Enabled) + { + autoProfilesTimer.Start(); + } + + btnStartStop.Text = Properties.Resources.StopText; + } + + private void serviceShutdown(bool log) + { + TaskRunner.Run(() => + { + Program.rootHub.Stop(log); + this.Invoke((System.Action)(() => { serviceShutdownFinish(); })); + }); + } + + private void serviceShutdownFinish() + { + hotkeysTimer.Stop(); + autoProfilesTimer.Stop(); + btnStartStop.Text = Properties.Resources.StartText; + blankControllerTab(); + populateFullNotifyText(); + } + public void btnStartStop_Clicked(bool log = true) { if (btnStartStop.Text == Properties.Resources.StartText) { - Program.rootHub.Start(log); - if (SwipeProfiles && !hotkeysTimer.Enabled) - { - hotkeysTimer.Start(); - } - - if (programpaths.Count > 0 && !autoProfilesTimer.Enabled) - { - autoProfilesTimer.Start(); - } - - btnStartStop.Text = Properties.Resources.StopText; + serviceStartup(log); } else if (btnStartStop.Text == Properties.Resources.StopText) { blankControllerTab(); - Program.rootHub.Stop(log); - hotkeysTimer.Stop(); - autoProfilesTimer.Stop(); - btnStartStop.Text = Properties.Resources.StartText; - blankControllerTab(); - populateFullNotifyText(); + serviceShutdown(log); } startToolStripMenuItem.Text = btnStartStop.Text; @@ -1004,7 +1029,7 @@ namespace DS4Windows if (!inHotPlug) { inHotPlug = true; - System.Threading.Tasks.Task.Run(() => { InnerHotplug2(uiContext); }); + TaskRunner.Run(() => { InnerHotplug2(uiContext); }); } } } @@ -1032,7 +1057,7 @@ namespace DS4Windows while (loopHotplug == true) { Program.rootHub.HotPlug(uiContext); - //Tasks.Task.Run(() => { Program.rootHub.HotPlug(uiContext); }); + //TaskRunner.Run(() => { Program.rootHub.HotPlug(uiContext); }); lock (hotplugCounterLock) { hotplugCounter--;