Added extra locking in TrayIconViewModel

This commit is contained in:
Travis Nickles 2019-12-26 15:00:37 -06:00
parent 9d04c0c177
commit ee34544f6f

View File

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Controls;
using DS4Windows;
@ -42,6 +42,7 @@ namespace DS4WinWPF.DS4Forms.ViewModels
public event EventHandler RequestOpen;
public event EventHandler RequestMinimize;
private ReaderWriterLockSlim _colLocker = new ReaderWriterLockSlim();
private List<ControllerHolder> controllerList = new List<ControllerHolder>();
private ProfileList profileListHolder;
private ControlService controlService;
@ -79,22 +80,28 @@ namespace DS4WinWPF.DS4Forms.ViewModels
private void ClearControllerList(object sender, EventArgs e)
{
_colLocker.EnterWriteLock();
controllerList.Clear();
_colLocker.ExitWriteLock();
}
private void UnhookEvents(object sender, EventArgs e)
{
_colLocker.EnterReadLock();
foreach (ControllerHolder holder in controllerList)
{
DS4Device currentDev = holder.Device;
RemoveDeviceEvents(currentDev);
}
_colLocker.ExitReadLock();
}
private void Service_HotplugController(ControlService sender, DS4Device device, int index)
{
SetupDeviceEvents(device);
_colLocker.EnterWriteLock();
controllerList.Add(new ControllerHolder(device, index));
_colLocker.ExitWriteLock();
}
private void ProfileListCol_CollectionChanged(object sender,
@ -115,6 +122,7 @@ namespace DS4WinWPF.DS4Forms.ViewModels
MenuItem item;
int idx = 0;
_colLocker.EnterReadLock();
foreach (ControllerHolder holder in controllerList)
{
DS4Device currentDev = holder.Device;
@ -156,6 +164,8 @@ namespace DS4WinWPF.DS4Forms.ViewModels
idx++;
}
_colLocker.ExitReadLock();
items.Add(item);
items.Add(new Separator());
item = new MenuItem() { Header = "Open" };
@ -188,7 +198,8 @@ namespace DS4WinWPF.DS4Forms.ViewModels
ProfileSelected?.Invoke(this, holder, item.Header.ToString());
}
private void DisconnectMenuItem_Click(object sender, System.Windows.RoutedEventArgs e)
private void DisconnectMenuItem_Click(object sender,
System.Windows.RoutedEventArgs e)
{
MenuItem item = sender as MenuItem;
int idx = Convert.ToInt32(item.Tag);
@ -214,11 +225,13 @@ namespace DS4WinWPF.DS4Forms.ViewModels
{
//IEnumerable<DS4Device> devices = DS4Devices.getDS4Controllers();
int idx = 0;
_colLocker.EnterWriteLock();
foreach (DS4Device currentDev in controlService.slotManager.ControllerColl)
{
controllerList.Add(new ControllerHolder(currentDev, idx));
idx++;
}
_colLocker.ExitWriteLock();
}
private void StartPopulateText(object sender, EventArgs e)
@ -234,12 +247,14 @@ namespace DS4WinWPF.DS4Forms.ViewModels
//IEnumerable<DS4Device> devices = DS4Devices.getDS4Controllers();
int idx = 1;
//foreach (DS4Device currentDev in devices)
_colLocker.EnterReadLock();
foreach (ControllerHolder holder in controllerList)
{
DS4Device currentDev = holder.Device;
items.Add($"{idx}: {currentDev.ConnectionType} {currentDev.Battery}%{(currentDev.Charging ? "+" : "")}");
idx++;
}
_colLocker.ExitReadLock();
TooltipText = string.Join("\n", items);
}
@ -248,11 +263,13 @@ namespace DS4WinWPF.DS4Forms.ViewModels
{
//IEnumerable<DS4Device> devices = DS4Devices.getDS4Controllers();
//foreach (DS4Device currentDev in devices)
_colLocker.EnterReadLock();
foreach (ControllerHolder holder in controllerList)
{
DS4Device currentDev = holder.Device;
SetupDeviceEvents(currentDev);
}
_colLocker.ExitReadLock();
}
private void SetupDeviceEvents(DS4Device device)
@ -274,6 +291,8 @@ namespace DS4WinWPF.DS4Forms.ViewModels
DS4Device currentDev = sender as DS4Device;
ControllerHolder item = null;
int idx = 0;
_colLocker.EnterWriteLock();
foreach (ControllerHolder holder in controllerList)
{
if (currentDev == holder.Device)
@ -291,6 +310,8 @@ namespace DS4WinWPF.DS4Forms.ViewModels
RemoveDeviceEvents(currentDev);
PopulateToolText();
}
_colLocker.ExitWriteLock();
}
private void HookEvents(object sender, EventArgs e)