EonaCat.HID/EonaCat.HID/DeviceEventMonitor.cs

54 lines
1.3 KiB
C#

using System.Threading.Tasks;
namespace EonaCat.HID
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
internal class DeviceEventMonitor
{
public event InsertedEventHandler Inserted;
public event RemovedEventHandler Removed;
public delegate void InsertedEventHandler();
public delegate void RemovedEventHandler();
private readonly Device _device;
private bool _wasConnected;
public DeviceEventMonitor(Device device)
{
_device = device;
}
public void Init()
{
Task.Run(EventMonitor);
}
private async Task EventMonitor()
{
while (_device.MonitorDeviceEvents)
{
var isConnected = _device.IsConnected;
if (isConnected != _wasConnected)
{
if (isConnected)
{
Inserted?.Invoke();
}
else
{
Removed?.Invoke();
}
_wasConnected = isConnected;
}
await Task.Delay(100);
}
}
}
}