Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Method: void SaveInBackground()

  • Declared in: RT.Serialization.Settings.SettingsFile<TSettings>

Declaration

public void SaveInBackground()

Summary

Saves settings on a background thread after a short delay. See Remarks.

Remarks

This method can be called as often as necessary. It is cheap to call frequently, and will only save at most once every SettingsFile<TSettings>.BackgroundSaveDelay seconds.

This method may be safely interleaved with calls to SettingsFile<TSettings>.Save(bool?). The saves are ordered as called; it is not possible for an older background save to overwrite a newer save from either method. The SettingsFile<TSettings>.Save(bool?) method attempts to cancel any pending background saves. Note that SettingsFile<TSettings>.Save(string, bool?) is completely exempt from this logic; it does not co-operate with background saving in any way and just writes out the settings to the specified file.

Save errors are ignored; it is not possible to detect a failed background save. However, on file sharing violation a new background save is scheduled automatically.

If the process exits with a background save pending, the background save is lost. Make sure to call SettingsFile<TSettings>.Save(bool?) prior to exiting the process.

If TSettings implements ICloneable the object is cloned on every call and it is the clone that is scheduled for saving. Otherwise the SettingsFile<TSettings>.Settings object is saved as-is, meaning that any changes made after the call to SettingsFile<TSettings>.SaveInBackground() may end up getting saved.

You must consider thread safety when modifying the SettingsFile<TSettings>.Settings object if you use this method. The background save thread expects to be able to read it consistently: in the ICloneable case it's at the time of this call on the calling thread; otherwise it's at an unspecified time on a thread pool thread. In particular, basic .NET collections such as Dictionary will break if read and modified simultaneously from multiple threads. You should either implement ICloneable and synchronize the call to this method with all changes to SettingsFile<TSettings>.Settings yourself, or you must wrap all changes to SettingsFile<TSettings>.Settings in a lock of SettingsFile<TSettings>.BackgroundLock. Alternatively you can make sure that all updates to SettingsFile<TSettings>.Settings are atomic (which non-concurrent .NET collections are not!)