/// <summary> /// Locks one or more subtrees in the Content Repository. If a subtree is locked, no modifications (Save operations) can be made there. /// Use this method with a using statement to make sure that the lock is released when not needed anymore. /// </summary> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="paths">One or more Content Repository paths to be locked.</param> /// <exception cref="LockedTreeException">Thrown when any of the requested paths (or any of the parent containers) are already locked.</exception> /// <returns>A Task that represents the asynchronous operation and wraps the new tree lock /// object containing the lock ids.</returns> public static async Task <TreeLock> AcquireAsync(CancellationToken cancellationToken, params string[] paths) { cancellationToken.ThrowIfCancellationRequested(); SnTrace.ContentOperation.Write("TreeLock: Acquiring lock for {0}", paths); var lockTasks = paths.Select(p => DataStore.AcquireTreeLockAsync(p, cancellationToken)); var lockIds = await Task.WhenAll(lockTasks).ConfigureAwait(false); for (var i = 0; i < lockIds.Length; i++) { cancellationToken.ThrowIfCancellationRequested(); if (lockIds[i] == 0) { await DataStore.ReleaseTreeLockAsync(lockIds, cancellationToken).ConfigureAwait(false); var msg = "Cannot acquire a tree lock for " + paths[i]; SnTrace.ContentOperation.Write("TreeLock: " + msg); throw new LockedTreeException(msg); } } var logOp = SnTrace.ContentOperation.StartOperation("TreeLock: {0} for {1}", lockIds, paths); return(new TreeLock(logOp, lockIds)); }
public static TreeLock Acquire(params string[] paths) { SnTrace.ContentOperation.Write("TreeLock: Acquiring lock for {0}", paths); var lockIds = paths.Select(p => DataStore.AcquireTreeLockAsync(p, CancellationToken.None).GetAwaiter().GetResult()) .ToArray(); for (var i = 0; i < lockIds.Length; i++) { if (lockIds[i] == 0) { DataStore.ReleaseTreeLockAsync(lockIds, CancellationToken.None).GetAwaiter().GetResult(); var msg = "Cannot acquire a tree lock for " + paths[i]; SnTrace.ContentOperation.Write("TreeLock: " + msg); throw new LockedTreeException(msg); } } var logOp = SnTrace.ContentOperation.StartOperation("TreeLock: {0} for {1}", lockIds, paths); return(new TreeLock(logOp, lockIds)); }