My.Settings in-memory cache synchronization limits
0 reputation · 23 Jun 2021, 19:47 UTC
0 reputation · 23 Jun 2021, 19:47 UTC
Visual Basic utilizes My.Settings as a wrapper for ApplicationSettingsBase, which loads persisted configuration values into an in-memory cache upon first access. This design ensures high-performance property retrieval throughout the application lifecycle.
Because the process relies on this internal cache, changes made to the user.config file by external processes or secondary application instances are not automatically reflected in the running process. The current in-memory state remains the authoritative source until a specific action is taken to synchronize with the disk.
In multi-instance scenarios, the Save and Reload methods create a potential for data loss due to a last-writer-wins conflict. If one instance modifies a setting while another instance holds a stale cached version, calling Save in the second instance may overwrite the first instance's changes without warning.
My.Settings.Reload provide a mechanism to merge external changes with unsaved in-memory modifications?34775 reputation · 24 Jun 2021, 05:55 UTC
No, My.Settings.Reload does not provide a mechanism to merge external changes with unsaved in-memory modifications. When Reload is called, the current in-memory cache is completely discarded and replaced by the values currently persisted in the user.config file. Any changes made to settings properties that have not yet been committed via Save will be lost.
Additionally, there is no built-in event or property within My.Settings or ApplicationSettingsBase to monitor the underlying configuration file for external changes. The framework assumes the application instance has exclusive ownership of its user settings during its lifecycle.
The following behaviors are established for the ApplicationSettingsBase implementation (typically used in .NET Framework and .NET Core/5+ Windows Forms and WPF applications):
Reload is explicitly invoked.To synchronize settings across multiple instances or detect external edits, you must implement a file system monitor. Because user.config is stored in a versioned local app data folder, you must first resolve the actual path to the file.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingProfile)` or similar to find the physical path of the user.config file.System.IO.FileSystemWatcher targeting the directory containing the config file, filtering for the specific filename.Changed event of the watcher, call My.Settings.Reload().' Example logic for the Changed event
Private Sub ConfigWatcher_Changed(sender As Object, e As FileSystemEventArgs)
' Note: FileSystemWatcher can fire multiple times for one save
My.Settings.Reload()
End Sub
To verify this behavior, manually edit the user.config file in a text editor while the application is running. Call My.Settings.Reload() and inspect the properties via the debugger to confirm the cache has updated.
Warning: Using a FileSystemWatcher in a multi-instance environment can lead to race conditions. If two instances are both watching the file and calling Save and Reload rapidly, you may encounter IOException errors due to file locking.
To provide a more specific concurrency strategy, please specify if your application instances are running on the same machine or across a network share.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.