private static SensorType WaitForSensor <SensorType>(SensorPort?port, CancellationToken token) where SensorType : ISensor { ManualResetEvent sensorAttached = new ManualResetEvent(false); SensorDetector detector = new SensorDetector(); bool run = true; bool checkForPort = port != null; SensorType newSensor = default(SensorType); detector.SensorAttached += delegate(ISensor sensor) { if (sensor.GetType() == typeof(SensorType)) { if (!checkForPort || sensor.Port == port.Value) { newSensor = (SensorType)sensor; sensorAttached.Set(); run = false; } } }; WaitHandle[] handles = new WaitHandle[] { sensorAttached, token.WaitHandle }; while (run) { detector.Update(); if (token.IsCancellationRequested) { run = false; } WaitHandle.WaitAny(handles, PollInterval); } return(newSensor); }
private void ListenThread() { while (run) { detector.Update(); terminate.WaitOne(interval); } }