Improved connect and disconnect handling with better cleanup and less exceptions.

This commit is contained in:
IceStormNG
2023-07-23 10:54:37 +02:00
parent 4ef7f5b4cb
commit d8b2836819

View File

@@ -16,41 +16,77 @@ namespace GHelper.Peripherals
return ConnectedMice.Count > 0; return ConnectedMice.Count > 0;
} }
public static void RefreshAllMice() public static void RefreshBatteryForAllDevices()
{ {
foreach (AsusMouse m in ConnectedMice) lock (ConnectedMice)
{ {
m.ReadBattery(); foreach (AsusMouse m in ConnectedMice)
{
if (!m.MouseIsReady)
{
m.SynchronizeMouse();
}
else
{
m.ReadBattery();
}
}
} }
} }
public static void Disconnect(AsusMouse am) public static void Disconnect(AsusMouse am)
{ {
ConnectedMice.Remove(am); lock (ConnectedMice)
if (DeviceChanged is not null)
{ {
DeviceChanged(am, EventArgs.Empty); ConnectedMice.Remove(am);
if (DeviceChanged is not null)
{
DeviceChanged(am, EventArgs.Empty);
}
} }
} }
public static void Connect(AsusMouse am) public static void Connect(AsusMouse am)
{ {
try lock (ConnectedMice)
{ {
am.Connect(); if (ConnectedMice.Contains(am))
} {
catch (IOException e) //Mouse already connected;
{ return;
Logger.WriteLine(am.GetDisplayName() + " failed to connect to device: " + e); }
return; try
} {
am.Connect();
}
catch (IOException e)
{
Logger.WriteLine(am.GetDisplayName() + " failed to connect to device: " + e);
return;
}
am.Disconnect += Mouse_Disconnect; am.Disconnect += Mouse_Disconnect;
am.SynchronizeMouse();
ConnectedMice.Add(am); //The Mouse might needs a few ms to register all its subdevices or the sync will fail.
if (DeviceChanged is not null) //Retry 3 times. Do not call this on main thread! It would block the UI
{
DeviceChanged(am, EventArgs.Empty); 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();
} }
} }
@@ -60,10 +96,23 @@ namespace GHelper.Peripherals
{ {
return; return;
} }
lock (ConnectedMice)
{
AsusMouse am = (AsusMouse)sender;
ConnectedMice.Remove(am);
Logger.WriteLine(am.GetDisplayName() + " reported disconnect. " + ConnectedMice.Count + " device are conneted.");
am.Dispose();
UpdateSettingsView();
}
}
AsusMouse am = (AsusMouse)sender;
ConnectedMice.Remove(am); private static void UpdateSettingsView()
am.Dispose(); {
Program.settingsForm.Invoke(delegate
{
Program.settingsForm.VisualizePeripherals();
});
} }
[MethodImpl(MethodImplOptions.Synchronized)] [MethodImpl(MethodImplOptions.Synchronized)]