예제 #1
0
    /// <summary>
    /// Open a channel to receive a message and send a result.
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TMessage">The type of the message.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="key">Specify a key to limit the channels to receive the message.</param>
    /// <param name="method">The delegate that is called when the message is sent.</param>
    /// <param name="weakReference">The weak reference of the object.<br/>
    /// The channel will be automatically closed when the object is garbage collected.<br/>
    /// To achieve maximum performance, you can set this value to null (DON'T forget to close the channel manually).</param>
    /// <returns>A new instance of XChannel.<br/>
    /// You need to call <see cref="XChannel.Dispose()"/> when the channel is no longer necessary, unless the weak reference is specified.</returns>
    public static XChannel OpenTwoWayKey <TKey, TMessage, TResult>(TKey key, Func <TMessage, TResult> method, object?weakReference = null)
        where TKey : notnull
    {
        var collection = Cache_KeyMessageResult <TKey, TMessage, TResult> .Collection;

        if (collection.CleanupCount++ >= CrossChannelConst.CleanupDictionaryThreshold)
        {
            lock (collection)
            {
                collection.CleanupCount = 1;
                collection.Cleanup();
            }
        }

        var channel = new XChannel_KeyMessageResult <TKey, TMessage, TResult>(collection, key, weakReference, method);

        return(channel);
    }
예제 #2
0
        /// <summary>
        /// Open a channel to receive a message and send a result.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="key">Specify a key to limit the channels to receive the message.</param>
        /// <param name="method">The delegate that is called when the message is sent.</param>
        /// <param name="weakReference">The weak reference of the object.<br/>
        /// The channel will be automatically closed when the object is garbage collected.<br/>
        /// To achieve maximum performance, you can set this value to null (DON'T forget to close the channel manually).</param>
        /// <returns>A new instance of XChannel.<br/>
        /// You need to call <see cref="XChannel.Dispose()"/> when the channel is no longer necessary, unless the weak reference is specified.</returns>
        public XChannel OpenTwoWayKey <TKey, TMessage, TResult>(TKey key, Func <TMessage, TResult> method, object?weakReference = null)
            where TKey : notnull
        {
            var collection = (XCollection_KeyMessageResult <TKey, TMessage, TResult>) this.dictionaryKeyMessageResult.GetOrAdd(
                new Identifier_KeyMessageResult(typeof(TKey), typeof(TMessage), typeof(TResult)),
                x => new XCollection_KeyMessageResult <TKey, TMessage, TResult>());

            if (collection.CleanupCount++ >= CrossChannelConst.CleanupDictionaryThreshold)
            {
                lock (collection)
                {
                    collection.CleanupCount = 1;
                    collection.Cleanup();
                }
            }

            var channel = new XChannel_KeyMessageResult <TKey, TMessage, TResult>(collection, key, weakReference, method);

            return(channel);
        }