Department of InformatiX
Microsoft .NET Micro Framework Tools & Resources
Core (2)
WPF (1)
Networking (4)
Emulators (2)
Port specifics


This is a simple example how to store, restore and delete global application settings:
public class Program { private static ExtendedWeakReference settingsReference; private static AppSettings Settings; public static void Main() { // 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 (settingsReferecnce.Target == null) Settings = new AppSettings(); else Settings = (AppSettings)settingsReference.Target; // EWR.Target is of type object Settings.RunCount++; // Change a settings value. settingsReference.Target = Settings; // This call writes the updated data into FLASH. Debug.Print(Settings.WelcomeMessage); Debug.Print("Number of starts: " + Settings.StartCount.ToString()); Thread.Sleep(5000); } public void SaveSettings() { // It is enough to set the Target property in order to write the data into FLASH. settingsReference.Target = Settings; } public void DeleteSettings() { // Data recovering and storing must be kept in pairs. // Calling Recover without setting Target frees the FLASH completely from this EWR. ExtendedWeakReference.Recover(typeof(AppSettings), 0); } public void RestoreLastSavedSettings() // Use this method to rewrite current settings using last saved data. { // First, mark the stored data as unrecovered so we can Recover them. settingsReference.PushBackIntoRecoverList(); // Try to recover them, but do not create them if they do not exist. ExtendedWeakReference restoreReference = ExtendedWeakReference.Recover(typeof(AppSettings), 0); if (restoreReference != null && restoreReference.Target != null) { Settings = (AppSettings)restoreReference.Target; // If they do, use them to refresh current settings. restoreReference.PushBackIntoRecoverList(); // Since we found the data, we have to put them back. } else Debug.Print("Could not restore settings."); } [Serializable] // If you want to store whole class, is has to be marked as serializable. private class AppSettings { public string WelcomeMessage = "Small is beautiful"; public int FontSize = 6; public int RunCount = 1; } }
To try this sample you have to use an emulator, which preserves the FLASH data between starts. You can use either the Advanced Emulator, or the EWRSampleEmulator, which is located at the ExtendedWeakReference folder of SDK samples.

Do I have to keep my data in class?

No. Any primitive type like int, string ... or array can be used as ExtendedWeakReference.Target, if enough.

Is there any limit to the complexity of the Target? What if I have an array in the class, or variables of another class type?

The whole class will be stored including referenced objects, objects to which these objects refers etc... assuming, that all referenced types are marked as serializable.

Do I have to create a separate class for the identifier?

No. If you are storing simple data and any other application is not running on the device, typeof(Program) will do. The idea is that if you declare your own class (can be empty) as private, any other application does not have access to this type, so it can't read your data.

Is ewr.Target = obj; enough to store the data in memory?

Yes. No other (strong) reference does prevent storing the data to the persistent storage, in opposite to standard WeakReference.

Will running garbage collector cause the data to be saved?

No. You have to set the ewr.Target property in order to save the data.

If ewr.Target == obj, does ewr.Target = obj; still persist the new data?


What happens to the original data if I set ewr.Target to new/different object? Do they still occupy the memory?

The original data are marked as deleted. For details, how .NET Micro Framework uses persistent memory and saves its lifetime, see the White Paper.

What is the ewr.PushBackIntoRecoverList() for?

Once you recover the data from persistent storage, you can't do it again any more. So if you find out that received data are not yours, you have to call PushBackIntoRecoverList(), so that the owner can recover them again. You also have to call this method, if you want to recover the data in another place in the application. This also means, that in order to call the Recover method with success, ewr.Target must be assigned before.

Do I have to recover and resave the data if I want to keep them for more then one boot?

As it was already said, you have to set ewr.Target before calling Recover. So if you don't recover, you don't have to just to keep the data to the next start.

How should I delete the stored data? Is setting the ewr.Target to null enough?

No. To completely free up the memory from your data, you need to call Recover without setting the Target property. Otherwise, the "headers" will still occupy the memory, just pointing to your null.

How do I find out that the storage memory is full?

There is currently no way to monitor or query the memory status. Neither any exception is thrown when there is no space to save the data to. You won't be just able to recover them.