From 3bdcf655fd5fefd6120abe9b030aa50795cca10d Mon Sep 17 00:00:00 2001 From: David Sparer Date: Mon, 25 Jun 2018 15:15:46 -0500 Subject: [PATCH 1/3] suspend tree model events during sort, since this can produce a lot of changes --- mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs | 4 ++++ mRemoteV1/Tree/ConnectionTreeModel.cs | 10 +++++++++- mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs index 91c76b983..6fa400563 100644 --- a/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs +++ b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Specialized; using System.ComponentModel; +using mRemoteNG.App; using mRemoteNG.Connection; +using mRemoteNG.Messages; namespace mRemoteNG.Config.Connections { @@ -46,6 +48,8 @@ namespace mRemoteNG.Config.Connections if (!mRemoteNG.Settings.Default.SaveConnectionsAfterEveryEdit) return; _connectionsService.SaveConnections(); + + Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, "Auto saved connections"); } } } diff --git a/mRemoteV1/Tree/ConnectionTreeModel.cs b/mRemoteV1/Tree/ConnectionTreeModel.cs index a4a16065e..a336aff4d 100644 --- a/mRemoteV1/Tree/ConnectionTreeModel.cs +++ b/mRemoteV1/Tree/ConnectionTreeModel.cs @@ -8,9 +8,11 @@ using mRemoteNG.Tree.Root; namespace mRemoteNG.Tree { - public sealed class ConnectionTreeModel : INotifyCollectionChanged, INotifyPropertyChanged + public sealed class ConnectionTreeModel : INotifyCollectionChanged, INotifyPropertyChanged { public List RootNodes { get; } = new List(); + public bool SuspendingCollectionChangedEvents { get; set; } + public bool SuspendingPropertyChangedEvents { get; set; } public void AddRootNode(ContainerInfo rootNode) { @@ -66,6 +68,9 @@ namespace mRemoteNG.Tree public event NotifyCollectionChangedEventHandler CollectionChanged; private void RaiseCollectionChangedEvent(object sender, NotifyCollectionChangedEventArgs args) { + if (SuspendingCollectionChangedEvents) + return; + CollectionChanged?.Invoke(sender, args); } @@ -73,6 +78,9 @@ namespace mRemoteNG.Tree private void RaisePropertyChangedEvent(object sender, PropertyChangedEventArgs args) { + if (SuspendingPropertyChangedEvents) + return; + PropertyChanged?.Invoke(sender, args); } } diff --git a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs index e7ca75784..df8711c28 100644 --- a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs +++ b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs @@ -311,11 +311,14 @@ namespace mRemoteNG.UI.Controls if (sortTarget == null) sortTarget = GetRootConnectionNode(); + var wasSuspendingEvents = _connectionTreeModel.SuspendingCollectionChangedEvents; + _connectionTreeModel.SuspendingCollectionChangedEvents = true; var sortTargetAsContainer = sortTarget as ContainerInfo; if (sortTargetAsContainer != null) sortTargetAsContainer.SortRecursive(sortDirection); else SelectedNode.Parent.SortRecursive(sortDirection); + _connectionTreeModel.SuspendingCollectionChangedEvents = wasSuspendingEvents; } /// From 2db6fabbe9e22cd7febb373aafec18a0cf37c7bb Mon Sep 17 00:00:00 2001 From: David Sparer Date: Wed, 18 Jul 2018 14:52:12 -0500 Subject: [PATCH 2/3] reverted 3bdcf65 --- mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs | 8 ++------ mRemoteV1/Tree/ConnectionTreeModel.cs | 8 -------- mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs | 3 --- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs index 6fa400563..4ff069e0f 100644 --- a/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs +++ b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Specialized; using System.ComponentModel; -using mRemoteNG.App; using mRemoteNG.Connection; -using mRemoteNG.Messages; namespace mRemoteNG.Config.Connections { @@ -22,10 +20,9 @@ namespace mRemoteNG.Config.Connections private void ConnectionsServiceOnConnectionsLoaded(object sender, ConnectionsLoadedEventArgs connectionsLoadedEventArgs) { - - connectionsLoadedEventArgs.NewConnectionTreeModel.CollectionChanged += ConnectionTreeModelOnCollectionChanged; connectionsLoadedEventArgs.NewConnectionTreeModel.PropertyChanged += ConnectionTreeModelOnPropertyChanged; + foreach (var oldTree in connectionsLoadedEventArgs.PreviousConnectionTreeModel) { oldTree.CollectionChanged -= ConnectionTreeModelOnCollectionChanged; @@ -47,9 +44,8 @@ namespace mRemoteNG.Config.Connections { if (!mRemoteNG.Settings.Default.SaveConnectionsAfterEveryEdit) return; - _connectionsService.SaveConnections(); - Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, "Auto saved connections"); + _connectionsService.SaveConnectionsAsync(); } } } diff --git a/mRemoteV1/Tree/ConnectionTreeModel.cs b/mRemoteV1/Tree/ConnectionTreeModel.cs index a336aff4d..502cf36ee 100644 --- a/mRemoteV1/Tree/ConnectionTreeModel.cs +++ b/mRemoteV1/Tree/ConnectionTreeModel.cs @@ -11,8 +11,6 @@ namespace mRemoteNG.Tree public sealed class ConnectionTreeModel : INotifyCollectionChanged, INotifyPropertyChanged { public List RootNodes { get; } = new List(); - public bool SuspendingCollectionChangedEvents { get; set; } - public bool SuspendingPropertyChangedEvents { get; set; } public void AddRootNode(ContainerInfo rootNode) { @@ -68,9 +66,6 @@ namespace mRemoteNG.Tree public event NotifyCollectionChangedEventHandler CollectionChanged; private void RaiseCollectionChangedEvent(object sender, NotifyCollectionChangedEventArgs args) { - if (SuspendingCollectionChangedEvents) - return; - CollectionChanged?.Invoke(sender, args); } @@ -78,9 +73,6 @@ namespace mRemoteNG.Tree private void RaisePropertyChangedEvent(object sender, PropertyChangedEventArgs args) { - if (SuspendingPropertyChangedEvents) - return; - PropertyChanged?.Invoke(sender, args); } } diff --git a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs index df8711c28..e7ca75784 100644 --- a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs +++ b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs @@ -311,14 +311,11 @@ namespace mRemoteNG.UI.Controls if (sortTarget == null) sortTarget = GetRootConnectionNode(); - var wasSuspendingEvents = _connectionTreeModel.SuspendingCollectionChangedEvents; - _connectionTreeModel.SuspendingCollectionChangedEvents = true; var sortTargetAsContainer = sortTarget as ContainerInfo; if (sortTargetAsContainer != null) sortTargetAsContainer.SortRecursive(sortDirection); else SelectedNode.Parent.SortRecursive(sortDirection); - _connectionTreeModel.SuspendingCollectionChangedEvents = wasSuspendingEvents; } /// From fe56268421a522d68323fdb9973308c4df07396b Mon Sep 17 00:00:00 2001 From: David Sparer Date: Wed, 18 Jul 2018 14:53:02 -0500 Subject: [PATCH 3/3] added a simple system to batch saves in the connection service --- mRemoteV1/Connection/ConnectionsService.cs | 41 +++++++++++++++++-- .../Controls/ConnectionTree/ConnectionTree.cs | 4 ++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/mRemoteV1/Connection/ConnectionsService.cs b/mRemoteV1/Connection/ConnectionsService.cs index 734915fb4..d1a35c025 100644 --- a/mRemoteV1/Connection/ConnectionsService.cs +++ b/mRemoteV1/Connection/ConnectionsService.cs @@ -8,6 +8,7 @@ using mRemoteNG.Config.Connections; using mRemoteNG.Config.Connections.Multiuser; using mRemoteNG.Config.Putty; using mRemoteNG.Connection.Protocol; +using mRemoteNG.Messages; using mRemoteNG.Security; using mRemoteNG.Tools; using mRemoteNG.Tree; @@ -19,6 +20,9 @@ namespace mRemoteNG.Connection { private static readonly object SaveLock = new object(); private readonly PuttySessionsManager _puttySessionsManager; + private bool _batchingSaves = false; + private bool _saveRequested = false; + private bool _saveAsyncRequested = false; public bool IsConnectionsFileLoaded { get; set; } public bool UsingDatabase { get; private set; } @@ -122,14 +126,27 @@ namespace mRemoteNG.Connection return newConnectionTreeModel; } + public void BeginBatchingSaves() + { + _batchingSaves = true; + } + + public void EndBatchingSaves() + { + _batchingSaves = false; + + if (_saveAsyncRequested) + SaveConnectionsAsync(); + else if(_saveRequested) + SaveConnections(); + } + /// /// Saves the currently loaded with /// no . /// public void SaveConnections() { - if (!IsConnectionsFileLoaded) - return; SaveConnections(ConnectionTreeModel, UsingDatabase, new SaveFilter(), ConnectionFileName); } @@ -143,10 +160,21 @@ namespace mRemoteNG.Connection /// public void SaveConnections(ConnectionTreeModel connectionTreeModel, bool useDatabase, SaveFilter saveFilter, string connectionFileName) { - if (connectionTreeModel == null) return; + if (connectionTreeModel == null) + return; + + if (!IsConnectionsFileLoaded) + return; + + if (_batchingSaves) + { + _saveRequested = true; + return; + } try { + Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, "Saving connections..."); RemoteConnectionsSyncronizer?.Disable(); var previouslyUsingDatabase = UsingDatabase; @@ -161,6 +189,7 @@ namespace mRemoteNG.Connection UsingDatabase = useDatabase; ConnectionFileName = connectionFileName; RaiseConnectionsSavedEvent(connectionTreeModel, previouslyUsingDatabase, UsingDatabase, connectionFileName); + Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, "Successfully saved connections"); } catch (Exception ex) { @@ -174,6 +203,12 @@ namespace mRemoteNG.Connection public void SaveConnectionsAsync() { + if (_batchingSaves) + { + _saveAsyncRequested = true; + return; + } + var t = new Thread(SaveConnectionsBGd); t.SetApartmentState(ApartmentState.STA); t.Start(); diff --git a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs index e7ca75784..c15f45b53 100644 --- a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs +++ b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs @@ -311,11 +311,15 @@ namespace mRemoteNG.UI.Controls if (sortTarget == null) sortTarget = GetRootConnectionNode(); + Runtime.ConnectionsService.BeginBatchingSaves(); + var sortTargetAsContainer = sortTarget as ContainerInfo; if (sortTargetAsContainer != null) sortTargetAsContainer.SortRecursive(sortDirection); else SelectedNode.Parent.SortRecursive(sortDirection); + + Runtime.ConnectionsService.EndBatchingSaves(); } ///