Пример #1
0
        public static async Task <int> CreateFactoriesAndChannelsUseAllOnceCloseAllAsync()
        {
            PoolOfAsyncThings <FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType> > oneTimeAsyncThing = null;
            var allTasksFromOneTimeAsyncThing = new List <Task <int> >();

            try
            {
                oneTimeAsyncThing = new PoolOfAsyncThings <FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType> >(
                    maxSize: s_test.TestParameters.MaxPooledFactories, // # of pooled FactoryAndPoolOfItsObjects
                    createInstance: () => new FactoryAndPoolOfItsAsyncObjects <ChannelFactory <ChannelType>, ChannelType>(
                        createFactoryInstance: () =>
                        s_test.CreateChannelFactory(),
                        destroyFactoryInstanceAsync: async(chf) =>
                        await s_test.CloseFactoryAsync(chf),
                        maxPooledObjects: s_test.TestParameters.MaxPooledChannels, // # of pooled channels within each pooled FactoryAndPoolOfItsObjects
                        createObject: (chf) =>
                        s_test.CreateChannel(chf),
                        destroyObjectAsync: async(ch) =>
                        await s_test.CloseChannelAsync(ch),
                        validateObjectInstance: s_test.ValidateChannel),
                    destroyInstanceAsync: (fapoiao) => fapoiao.DestroyAsync(),
                    instanceValidator: (fapoiao) => s_test.ValidateFactory(fapoiao.Factory));

                foreach (var factoryAndPoolOfItsChannels in oneTimeAsyncThing.GetAllPooledInstances())
                {
                    foreach (var channel in factoryAndPoolOfItsChannels.ObjectsPool.GetAllPooledInstances())
                    {
                        allTasksFromOneTimeAsyncThing.Add(s_test.UseAsyncChannel()(channel));
                    }
                }
            }
            finally
            {
                if (oneTimeAsyncThing != null)
                {
                    await Task.WhenAll(allTasksFromOneTimeAsyncThing);

                    await oneTimeAsyncThing.DestoryAllPooledInstancesAsync();
                }
            }
            return(allTasksFromOneTimeAsyncThing.Sum(t => t.Result));
        }
 public static async Task RecyclePooledFactoriesAsync()
 {
     Console.WriteLine("RecyclePooledFactories");
     await s_recyclablePooledFactoriesAndChannels.DestoryAllPooledInstancesAsync();
 }
Пример #3
0
 public static async Task RecycleFactoriesAsync()
 {
     await s_recyclablePooledChannelFactories.DestoryAllPooledInstancesAsync();
 }