Hi. I’m trying to use ExtendedWeakReference to save some settings data for my program. I am using the following code:
public static void WriteData(AppSettings flashAppSettings)
{
// Reads data from FLASH memory.
// If no entry with AppSettings/0 identifier exists in memory, a new one is created.
settingsReference = ExtendedWeakReference.RecoverOrCreate(typeof(AppSettings), 0, ExtendedWeakReference.c_SurvivePowerdown);
// If the entry was just created, its value is set to null.
if (settingsReference.Target == null)
Settings = new AppSettings();
else
Settings = (AppSettings)settingsReference.Target; // EWR.Target is of type object
Settings = flashAppSettings;// Change a settings value.
settingsReference.Target = Settings; // This call writes the updated data into FLASH.
}
[Serializable] // If you want to store whole class, is has to be marked as serializable.
public class AppSettings
{
public int BaseData = 7000;
public int BaseNum = 2;
}
public static AppSettings Access()
{
//************************************************************************************
//** FLASH MEMORY ACCESS - BEGIN ***
//************************************************************************************
// Reads data from FLASH memory.
// If no entry with AppSettings/0 identifier exists in memory, a new one is created.
settingsReference = ExtendedWeakReference.RecoverOrCreate(typeof(AppSettings), 0, ExtendedWeakReference.c_SurvivePowerdown);
// If the entry was just created, its value is set to null.
if (settingsReference.Target == null)
Settings = new AppSettings();
else
Settings = (AppSettings)settingsReference.Target; // EWR.Target is of type object
return (Settings);
//************************************************************************************
//** FLASH MEMORY ACCESS - END ***
//************************************************************************************
}
I am able to write the data once and when I read it I see my default values of 7000 and 2. However, If I change these numbers via code such as:
AppSettings myAppData = Access();
myAppData.BaseData = 5555;
myAppData.BaseNum = 2;
WriteData(myAppData);
Thread.Sleep(3000);
the code runs successfully, but my next call to Access returns a copy of the default settings and not the new data that I saved even though I see the correct values via my debugger right before the
settingsReference.Target = Settings;
Also, in reading about EWR… it appears that I may need to save this data back to flash after each read – is this correct? This doesn’t sound very practical.
Thank you.