예제 #1
0
        /// <summary>
        /// Add disposable action
        /// </summary>
        /// <param name="name"></param>
        /// <param name="disposableAction"></param>
        protected void AddDisposableAction(string name, DisposableAction disposableAction)
        {
            CheckDisposed();

            if (!ContainDisposableAction(name))
            {
                _anonymousDisposableObjects.Add(name, AnonymousDisposableObject.Create(disposableAction));
            }
        }
예제 #2
0
        /// <summary>
        /// Add disposable action
        /// </summary>
        /// <param name="name"></param>
        /// <param name="action"></param>
        protected void AddDisposableAction(string name, DisposableAction action)
        {
            if (_disposableActionRegister.Contains(name))
            {
                return;
            }

            if (action != null)
            {
                var disposable = AnonymousDisposableObject.Create(action);
                _collectionDisposableObjects.Add(disposable);
                _disposableActionRegister.Add(name);
            }
        }
예제 #3
0
 /// <summary>
 /// Attempts to update context of disposable action stored in this field, Return <c>false</c> if the field is <c>null</c>.
 /// </summary>
 /// <param name="contextUpdater"></param>
 /// <returns></returns>
 public bool TryUpdateContext(Func <T, T> contextUpdater)
 {
     while (true)
     {
         var origin = Interlocked.CompareExchange(ref _field, _field, _field);
         if (origin == null)
         {
             return(false);
         }
         var updatedContext = new DisposableAction <T>(origin, contextUpdater);
         var ret            = Interlocked.CompareExchange(ref _field, updatedContext, origin);
         if (ReferenceEquals(origin, ret))
         {
             return(true);
         }
     }
 }
 /// <summary>
 /// Create a new disposable that executes dispose when disposed.
 /// </summary>
 /// <param name="disposableAction"></param>
 /// <returns></returns>
 public static AnonymousDisposableObject Create(DisposableAction disposableAction) => new AnonymousDisposableObject(disposableAction);
 /// <summary>
 /// Add dispose <see cref="Action"/>.
 /// </summary>
 /// <param name="disposableAction"></param>
 public void Add(DisposableAction disposableAction)
 {
     Add(disposableAction?.InternalAction);
 }
 /// <summary>
 /// Create a new <see cref="AnonymousDisposableObject"/> instance.
 /// </summary>
 /// <param name="disposableAction"></param>
 public AnonymousDisposableObject(DisposableAction disposableAction) : base(disposableAction?.InternalAction)
 {
 }
예제 #7
0
 public DisposableActionField(Action <T> action, T context)
 {
     _field = new DisposableAction <T>(action, context);
 }
예제 #8
0
 /// <summary>
 /// Create a new <see cref="DisposableAction{T}"/> instance.
 /// </summary>
 /// <param name="originalDisposableAction"></param>
 /// <param name="contextUpdater"></param>
 public DisposableAction(DisposableAction <T> originalDisposableAction, Func <T, T> contextUpdater)
     : base(originalDisposableAction.Action, originalDisposableAction.Context, contextUpdater)
 {
 }
예제 #9
0
 public DisposableAction(DisposableAction <T> originalDisposableAction, Func <T, T> contextUpdater)
 {
     _action  = originalDisposableAction._action;
     _context = contextUpdater(originalDisposableAction._context);
 }