/// <summary>
        /// Registers the passed context with a collection that can be retried with subsequent calls to TryGetValue.
        ///
        /// Returns false if the passed context object is already registered.
        /// </summary>
        public bool TryCreate(object ctx)
        {
            var cell = ProfileContextCell.ToStoreUnder(ctx);

            // we can't pass this as a delegate, because TryAdd may invoke the factory multiple times,
            //   which would lead to over allocation.
            var storage = ConcurrentProfileStorageCollection.GetOrCreate();

            return(profiledCommands.TryAdd(cell, storage));
        }
Beispiel #2
0
        /// <summary>
        /// Removes a context, setting all commands to a (non-thread safe) enumerable of
        /// all the commands attached to that context.
        ///
        /// If the context was never registered, will return false and set commands to null.
        ///
        /// Subsequent calls to TryRemove with the same context will return false unless it is
        /// re-registered with TryCreate.
        /// </summary>
        /// <param name="ctx">The context to remove for.</param>
        /// <param name="commands">The commands to remove.</param>
        public bool TryRemove(object ctx, out ProfiledCommandEnumerable commands)
        {
            var cell = ProfileContextCell.ToLookupBy(ctx);

            if (!profiledCommands.TryRemove(cell, out ConcurrentProfileStorageCollection storage))
            {
                commands = default(ProfiledCommandEnumerable);
                return(false);
            }

            commands = storage.EnumerateAndReturnForReuse();
            return(true);
        }
        /// <summary>
        /// Returns true and sets val to the tracking collection associated with the given context if the context
        /// was registered with TryCreate.
        ///
        /// Otherwise returns false and sets val to null.
        /// </summary>
        public bool TryGetValue(object ctx, out ConcurrentProfileStorageCollection val)
        {
            var cell = ProfileContextCell.ToLookupBy(ctx);

            return(profiledCommands.TryGetValue(cell, out val));
        }