diff --git a/DirectInput/InputDeviceList.cs b/DirectInput/InputDeviceList.cs index bcd69327..75c267b0 100644 --- a/DirectInput/InputDeviceList.cs +++ b/DirectInput/InputDeviceList.cs @@ -106,20 +106,27 @@ private void waitthread() System.Diagnostics.Debug.WriteLine("IDL start"); - while (true) + while (!stophandle.WaitOne(0)) { - int hhit = System.Threading.WaitHandle.WaitAny(wh); + try + { + int hhit = System.Threading.WaitHandle.WaitAny(wh); - if (hhit == inputdevices.Count) // stop! - break; + if (hhit == inputdevices.Count) // stop! + break; - List list = inputdevices[hhit].GetEvents(); + List list = inputdevices[hhit].GetEvents(); - if (list != null) - { - //System.Diagnostics.Debug.WriteLine(Environment.TickCount + " Handle hit " + hhit + " " + inputdevices[hhit].ID().Name); + if (list != null && !stophandle.WaitOne(0)) + { + //System.Diagnostics.Debug.WriteLine(Environment.TickCount + " Handle hit " + hhit + " " + inputdevices[hhit].ID().Name); - invokeAsyncOnUiThread(() => { OnNewEvent?.Invoke(list); }); // call in action context. + invokeAsyncOnUiThread(() => { OnNewEvent?.Invoke(list); }); // call in action context. + } + } + catch (Exception ex) + { + System.Diagnostics.Trace.WriteLine($"Error in InputDeviceList.waitthread: {ex}"); } } }