示例#1
0
        public static IEnumerable <SynDevice> FindDevices(this ISynAPI synApi, ConnectionType connectionType, DeviceType deviceType)
        {
            int handle  = 0;
            var devices = new List <SynDevice>();

            while (true)
            {
                try {
                    synApi.FindDevice((int)connectionType, (int)deviceType, ref handle);
                } catch (FileNotFoundException) {
                    return(devices);
                }
                synApi.CreateDevice(handle, out SynDevice device);
                devices.Add(device);
            }
        }
        public void WorkLoop()
        {
            try {
                synapi = new SynAPI();
            } catch (COMException e) {
                // COMException here indicates that SynAPI is not installed.
                // Throw an exception to indicate this to the caller.
                throw new COMInitializationFailedException("Synaptics driver version incorrect or not installed", e);
            }
            synapi.Initialize();
            synapi.SetEventNotification(apiEvent.SafeWaitHandle);

            while (true)
            {
                var deviceHandlers = EnumerateDevices()
                                     .Select(DeviceHandler.Create)
                                     .ToList();

                DevicesEnumerated?.Invoke(deviceHandlers.Count);

                var waitHandles = deviceHandlers
                                  .Select(handler => handler.Event)
                                  .Append(apiEvent)
                                  .ToArray();

                while (true)
                {
                    var handleIdx = WaitHandle.WaitAny(waitHandles);
                    if (handleIdx < deviceHandlers.Count)
                    {
                        // Handle packets from this device.
                        deviceHandlers[handleIdx].HandlePackets();
                    }
                    else
                    {
                        // Reinitialize devices.
                        Console.WriteLine("Reinitializing devices.");
                        break;
                    }
                }
                deviceHandlers.ForEach(device => device.Dispose());
            }
        }