Extended Weak Reference not saving new data

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.

I have requested a Flush() method to be added to EWR more than once as I personally see ti very valuable. Maybe you can ask fro it as well before NETMF 4.2 is out :slight_smile: www.netmf.com

Now, for your questions, EWR is saved on idle system. i think if you do a soft reboot then the data is flushed and guranteed. And no you do not have to write the data every time you read it!

Thank you. I haven’t figured out how to do a soft reboot by code yet, but I finally did find a reference you made in another post to the following function.

GHIElectronics.NETMF.System.Util.FlushExtendedWeakReferences();

I’ll try this as well and hopefully it will do the trick. I will also mention this on the netmf site.

Thank you.

Oh! So GHI actually added this already! I didn’t know this…good job GHI team!

For anyone else wanting to do a soft reboot, you can reference this page:

or

RebootDevice within Microsoft.Spot.Hardware

public static void RebootDevice (bool soft, int exeConstraintTimeout_ms)

Parameters
[italic]soft [/italic]
true indicates that the reboot request is for a soft reboot; false indicates hard reboot.
Some devices might not support soft reboot.
[italic]exeConstraintTimeout_ms [/italic]
Execution constraint timeout (in milliseconds) for the event handlers.
If the event handlers take longer than the given value, then the handlers
will be aborted and the reboot will be executed.

Hi,

Does

SystemUpdate.AccessBootloader()

constitute as a soft reboot?

As i am writing to EWR then immediately accessing bootloader, can i guarantee that the data will be saved?