From 6d4527a4c67b5dc87749eab502756433e38d0457 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Sat, 3 Jun 2017 15:16:59 -0700 Subject: [PATCH] Use SpinWait instead of Sleep for Exclusive mode routine --- DS4Windows/DS4Library/DS4Devices.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/DS4Windows/DS4Library/DS4Devices.cs b/DS4Windows/DS4Library/DS4Devices.cs index daba4e3..2cfe1d9 100644 --- a/DS4Windows/DS4Library/DS4Devices.cs +++ b/DS4Windows/DS4Library/DS4Devices.cs @@ -192,6 +192,7 @@ namespace DS4Windows public static void reEnableDevice(string deviceInstanceId) { + Stopwatch sw = new Stopwatch(); bool success; Guid hidGuid = new Guid(); NativeMethods.HidD_GetHidGuid(ref hidGuid); @@ -226,7 +227,14 @@ namespace DS4Windows throw new Exception("Error disabling device, error code = " + Marshal.GetLastWin32Error()); } - System.Threading.Thread.Sleep(50); + sw.Start(); + while (sw.ElapsedMilliseconds < 50) + { + // Use SpinWait to keep control of current thread. Using Sleep could potentially + // cause other events to get run out of order + System.Threading.Thread.SpinWait(100); + } + sw.Stop(); propChangeParams.stateChange = NativeMethods.DICS_ENABLE; success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams));