mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Improved connect and disconnect handling with better cleanup and less exceptions.
This commit is contained in:
@@ -16,15 +16,28 @@ namespace GHelper.Peripherals
|
||||
return ConnectedMice.Count > 0;
|
||||
}
|
||||
|
||||
public static void RefreshAllMice()
|
||||
public static void RefreshBatteryForAllDevices()
|
||||
{
|
||||
lock (ConnectedMice)
|
||||
{
|
||||
foreach (AsusMouse m in ConnectedMice)
|
||||
{
|
||||
if (!m.MouseIsReady)
|
||||
{
|
||||
m.SynchronizeMouse();
|
||||
}
|
||||
else
|
||||
{
|
||||
m.ReadBattery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void Disconnect(AsusMouse am)
|
||||
{
|
||||
lock (ConnectedMice)
|
||||
{
|
||||
ConnectedMice.Remove(am);
|
||||
if (DeviceChanged is not null)
|
||||
@@ -32,9 +45,17 @@ namespace GHelper.Peripherals
|
||||
DeviceChanged(am, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Connect(AsusMouse am)
|
||||
{
|
||||
lock (ConnectedMice)
|
||||
{
|
||||
if (ConnectedMice.Contains(am))
|
||||
{
|
||||
//Mouse already connected;
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
am.Connect();
|
||||
@@ -46,12 +67,27 @@ namespace GHelper.Peripherals
|
||||
}
|
||||
|
||||
am.Disconnect += Mouse_Disconnect;
|
||||
|
||||
//The Mouse might needs a few ms to register all its subdevices or the sync will fail.
|
||||
//Retry 3 times. Do not call this on main thread! It would block the UI
|
||||
|
||||
int tries = 0;
|
||||
while (!am.MouseIsReady && tries < 3)
|
||||
{
|
||||
Thread.Sleep(250);
|
||||
Logger.WriteLine(am.GetDisplayName() + " synchronising. Try " + (tries + 1));
|
||||
am.SynchronizeMouse();
|
||||
++tries;
|
||||
}
|
||||
|
||||
ConnectedMice.Add(am);
|
||||
Logger.WriteLine(am.GetDisplayName() + " added to the list: " + ConnectedMice.Count + " device are conneted.");
|
||||
if (DeviceChanged is not null)
|
||||
{
|
||||
DeviceChanged(am, EventArgs.Empty);
|
||||
}
|
||||
UpdateSettingsView();
|
||||
}
|
||||
}
|
||||
|
||||
private static void Mouse_Disconnect(object? sender, EventArgs e)
|
||||
@@ -60,10 +96,23 @@ namespace GHelper.Peripherals
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (ConnectedMice)
|
||||
{
|
||||
AsusMouse am = (AsusMouse)sender;
|
||||
ConnectedMice.Remove(am);
|
||||
Logger.WriteLine(am.GetDisplayName() + " reported disconnect. " + ConnectedMice.Count + " device are conneted.");
|
||||
am.Dispose();
|
||||
UpdateSettingsView();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void UpdateSettingsView()
|
||||
{
|
||||
Program.settingsForm.Invoke(delegate
|
||||
{
|
||||
Program.settingsForm.VisualizePeripherals();
|
||||
});
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||
|
||||
Reference in New Issue
Block a user