Beispiel #1
0
            async Task Synchronize(ModelRepresentation targetRep, bool dataLocked)
            {
                ModelRepresentation sourceModel = null;
                int currentChangeVersion;

                lock (changeLock) {
                    sourceModel          = lastChangedRepresentation;
                    currentChangeVersion = changeVersion;
                }

                if (targetRep.CurrentVersion == currentChangeVersion || sourceModel == null || sourceModel == targetRep)
                {
                    return;                     // Not changed
                }
                try {
                    // Any operation that requires the lock of several representation must take
                    // the model lock first, to avoid deadlocks
                    if (!dataLocked)
                    {
                        await dataLock.WaitAsync();

                        await targetRep.WaitHandle.WaitAsync();

                        await sourceModel.WaitHandle.WaitAsync();
                    }

                    await targetRep.InternalCopyFrom(sourceModel);
                } finally {
                    if (!dataLocked)
                    {
                        dataLock.Release();
                        targetRep.WaitHandle.Release();
                        sourceModel.WaitHandle.Release();
                    }
                }
                RaiseChangedEvent(targetRep.GetType());
            }