Threading Kills ExtendedWeakReferences in MF 4.2

I just converted my Cobra project from MF 4.1 to 4.2 and have discovered a serious bug in MS ExtendedWeakReference. I’m using EWR for persistent storage and in MF 4.2 if you have a thread running, EWR will not save to persistent memory. I have tried every possible thing I can think of to get it to work but to no avail. I would greatly appreciate any suggestions or a confirmation that EWR is useless in 4.2. Here is my code. It’s the same for Cobra or Cobra II. If you comment out the DummyThread.Start, it will work but if you start the thread it does not save the data.


    public class Program
    {
        static Thread DummyThread;
        private static ExtendedWeakReference ExtendedWeakReferenceStorage;
        private static Byte[] TargetArray;
        private static Byte[] DataArray = new Byte[1024];

        public static void Main()
        {
            Thread.Sleep(5000); // allow time to start MFDeploy

            // Get data from persistent storage
            ExtendedWeakReferenceStorage = ExtendedWeakReference.RecoverOrCreate(typeof(Array), 0, (ExtendedWeakReference.c_SurviveBoot + ExtendedWeakReference.c_SurvivePowerdown));
            ExtendedWeakReferenceStorage.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;
            TargetArray = (Byte[])ExtendedWeakReferenceStorage.Target;
            if (TargetArray != null)
                Array.Copy(TargetArray, DataArray, DataArray.Length);
            Debug.Print("Get =" + DataArray[0].ToString());

            // Update data
            DataArray[0]++;

            // Save data to persistent storage
            ExtendedWeakReferenceStorage.Target = DataArray;
            Debug.Print("Save=" + DataArray[0].ToString());
            Thread.Sleep(2000); // wait for data to be physically saved (doesn't help with the bug)

            // Start thread to show bug in saving data
            DummyThread = new Thread(StartThread);
            DummyThread.Start();
            Debug.Print("End of Main");
        }

        private static void StartThread()
        {
            Debug.Print("Starting Thread");
            while (true)
            {
                Thread.Sleep(1);
            }
        }
    }

Try Util.FlushExtendedWeakReferences() in GHI.Premium.System.

That worked. Thanks a bunch.

I found another quirk with the MF 4.2 implementation of EWR. What ever you assigned to the Target remains the same as the first assignment. Any changes you make to the data of the Target will not occur. So you have to copy your data to a new instance of a target variable, then assign it to Target. Here is the changed code for saving data:


            // Update data
            DataArray[0]++;

            // Copy data to a new target array
            TargetArray = new Byte[DataArray.Length];
            Array.Copy(DataArray, TargetArray, DataArray.Length);

            // Save data to persistent storage
            ExtendedWeakReferenceStorage.Target = TargetArray;
            Util.FlushExtendedWeakReferences();

I am not sure this is an issue. EWR does not look at the contents of your target data. The only way it knows a change has been made is when you change the Target reference.

You’re right, same behavior is exhibited in MF 4.1. I’ve been working on the USBizi for the last two years and just came back to the Cobra with MF 4.2 so was a little rusty with EWR.

But still good to know since I was baffled at the statement DataArray[0]++ not doing anything.