/// <summary>
        /// Creates a pooled worker.
        /// </summary>
        /// <param name="worker">The worker which is being managed by the pool.</param>
        /// <returns>The pooled worker instance.</returns>
        protected virtual IWorker CreatePooledWorker(IRuntimeWorker worker)
        {
            if (worker == null)
            {
                throw new ArgumentNullException(nameof(worker));
            }

            return(new PooledWorker(cache, worker));
        }
示例#2
0
        /// <inheritdoc />
        public void Release(IRuntimeWorker worker)
        {
            if (worker == null)
            {
                throw new ArgumentNullException(nameof(worker));
            }

            GuardMustNotBeDisposed();
            WaitIfLocked();

            if (busy.TryRemove(worker, out var entry))
            {
                entry.Worker.Reset();

                available.Enqueue(entry);
            }
        }
        /// <inheritdoc />
        public IWorker Get(Action onRunCallback, Action postCompletedCallback)
        {
            if (onRunCallback == null)
            {
                throw new ArgumentNullException(nameof(onRunCallback));
            }

            GuardMustNotBeDisposed();

            IRuntimeWorker worker = null;

            try
            {
                worker = cache.Get();
                if (worker != null)
                {
                    try
                    {
                        worker.Initialize(new WorkerExecutionContext
                        {
                            OnRunCallback         = onRunCallback,
                            PostCompletedCallback = postCompletedCallback
                        });

                        return(CreatePooledWorker(worker));
                    }
                    catch (Exception)
                    {
                        worker.Reset();
                        throw;
                    }
                }
            }
            catch (Exception)
            {
                if (worker != null)
                {
                    cache.Release(worker);
                }

                throw;
            }

            return(null);
        }
示例#4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PooledWorker"/> class.
 /// </summary>
 /// <param name="cache">The cache which sourced the worker.</param>
 /// <param name="worker"></param>
 public PooledWorker(IWorkerCache cache, IRuntimeWorker worker)
 {
     this.cache  = cache ?? throw new ArgumentNullException(nameof(cache));
     this.worker = worker ?? throw new ArgumentNullException(nameof(worker));
 }
示例#5
0
        protected override IWorker CreatePooledWorker(IRuntimeWorker worker)
        {
            OnCreatePooledWorkerCallback?.Invoke(worker);

            return(base.CreatePooledWorker(OnBaseCreatePooledWorkerCallback(worker)));
        }