From 481bad9d8d5dec7d278adf48eb1673af32ebe07f Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Thu, 17 Dec 2015 22:21:55 -0500 Subject: [PATCH] Optimize the update of the menu. Only on demand. Update images of the menu in Main thread. See #41 --- SoundSwitch/Util/TrayIcon.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/SoundSwitch/Util/TrayIcon.cs b/SoundSwitch/Util/TrayIcon.cs index 7ab96f8920..945e3b8370 100644 --- a/SoundSwitch/Util/TrayIcon.cs +++ b/SoundSwitch/Util/TrayIcon.cs @@ -35,6 +35,7 @@ public sealed class TrayIcon : IDisposable private readonly ContextMenuStrip _selectionMenu = new ContextMenuStrip(); private readonly ContextMenuStrip _settingsMenu = new ContextMenuStrip(); private readonly SynchronizationContext _context = SynchronizationContext.Current ?? new SynchronizationContext(); + private bool _needToUpdateList = true; private readonly NotifyIcon _trayIcon = new NotifyIcon { Icon = Icon.FromHandle(Resources.SoundSwitch16.GetHicon()), @@ -60,6 +61,7 @@ public TrayIcon() { if (e.Button == MouseButtons.Left) { + UpdateDeviceSelectionList(); _trayIcon.ContextMenuStrip = _selectionMenu; var mi = typeof (NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic); @@ -136,24 +138,24 @@ private void SetEventHandlers() default: throw new ArgumentOutOfRangeException(); } - - UpdateImageContextMenu(audioDeviceType, audioChangeEvent); + _context.Send(s => { UpdateImageContextMenu(audioDeviceType, audioChangeEvent); }, null); + }; AppModel.Instance.SelectedDeviceChanged += - (sender, deviceListChanged) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); }; + (sender, @event) => { _needToUpdateList = true; }; AppModel.Instance.NewVersionReleased += (sender, @event) => { _context.Send(s => { NewReleaseAvailable(sender, @event); }, null); }; AppModel.Instance.DeviceRemoved += - (sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); }; + (sender, @event) => { _needToUpdateList = true; }; AppModel.Instance.DeviceAdded += - (sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); }; + (sender, @event) => { _needToUpdateList = true; }; AppModel.Instance.DeviceStateChanged += - (sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); }; + (sender, @event) => { _needToUpdateList = true; }; AppModel.Instance.InitializeMain(); } @@ -198,6 +200,11 @@ public void UpdateDeviceSelectionList() { using (AppLogger.Log.InfoCall()) { + if (!_needToUpdateList) + { + AppLogger.Log.Info("Device list doesn't need update"); + return; + } if (AppModel.Instance.AvailablePlaybackDevices.Count < 0 && AppModel.Instance.AvailableRecordingDevices.Count < 0) { @@ -220,6 +227,7 @@ public void UpdateDeviceSelectionList() _selectionMenu.Items.Add(new ToolStripDeviceItem(DeviceClicked, item)); } } + _needToUpdateList = false; } }