public Task EnqueueTask(Action action, CancellationToken?cancellationToken = null,
                                QueueExceptionHandler exceptionHandler             = null
                                )
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);

            var completionSource = new TaskCompletionSource <object>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, action, null, exceptionHandler));

            return(completionSource.Task);
        }
 public WorkItem(
     dynamic completionSource, CancellationToken?cancellationToken, dynamic function, dynamic state = null,
     QueueExceptionHandler exceptionHandler = null
     ) : this()
 {
     this.CompletionSource  = completionSource;
     this.CancellationToken = cancellationToken;
     this.Function          = function;
     this.ResultType        = null;
     this.State             = state;
     this.ExceptionHandler  = exceptionHandler;
 }
        public Task <TResult> EnqueueTask <TState, TResult>(
            Func <TState, TResult> function, TState state, CancellationToken?cancellationToken = null,
            QueueExceptionHandler exceptionHandler = null
            )
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);

            var completionSource = new TaskCompletionSource <TResult>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, function, state, exceptionHandler));

            return(completionSource.Task);
        }
        public Task EnqueueTask(Action action, CancellationToken?cancellationToken = null,
                                QueueExceptionHandler exceptionHandler             = null
                                )
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            var completionSource = new TaskCompletionSource <object>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, action, null, exceptionHandler));

            return(completionSource.Task);
        }
            public WorkItem(
                dynamic completionSource, CancellationToken?cancellationToken, dynamic function, dynamic state = null,
                QueueExceptionHandler exceptionHandler = null
                ) : this()
            {
                //Contract.Requires<ArgumentNullException>(completionSource != null);
                //Contract.Requires<ArgumentNullException>(function != null);

                this.CompletionSource  = completionSource;
                this.CancellationToken = cancellationToken;
                this.Function          = function;
                this.ResultType        = null;
                this.State             = state;
                this.ExceptionHandler  = exceptionHandler;
            }
        public Task <TResult> EnqueueTask <TState, TResult>(
            Func <TState, TResult> function, TState state, CancellationToken?cancellationToken = null,
            QueueExceptionHandler exceptionHandler = null
            )
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            var completionSource = new TaskCompletionSource <TResult>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, function, state, exceptionHandler));

            return(completionSource.Task);
        }
        public Task <TResult> EnqueueTask <TResult>(
            Func <TResult> function, CancellationToken?cancellationToken = null, QueueExceptionHandler exceptionHandler = null
            )
        {
            //Contract.Requires<ObjectDisposedException>(!this.IsDisposed);
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(this.IsDisposed.ToString());
            }

            var completionSource = new TaskCompletionSource <TResult>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, function, null, exceptionHandler));

            return(completionSource.Task);
        }
        public Task EnqueueTask <TState>(
            Action <TState> action, TState state, CancellationToken?cancellationToken = null,
            QueueExceptionHandler exceptionHandler = null
            )
        {
            //Contract.Requires<ObjectDisposedException>(!this.IsDisposed);
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(this.IsDisposed.ToString());
            }

            var completionSource = new TaskCompletionSource <object>();

            this.queuedItems.Add(new WorkItem(completionSource, cancellationToken, action, state, exceptionHandler));

            return(completionSource.Task);
        }
        public AsyncWorkQueue(
            string threadName     = "Async Work Queue Thread", ThreadPriority threadPriority = ThreadPriority.BelowNormal,
            int workerThreadCount = 1, QueueExceptionHandler defaultExceptionHandler         = null
            )
        {
            this.workerTimeoutMs         = -1;
            this.workerTokenSource       = new CancellationTokenSource();
            this.queuedItems             = new BlockingCollection <WorkItem>();
            this.workers                 = new Thread[workerThreadCount];
            this.defaultExceptionHandler = defaultExceptionHandler;

            for (int i = 0; i < workerThreadCount; i++)
            {
                Thread worker = new Thread(this.ProcessWorkItems)
                {
                    IsBackground = false
                };
                this.workers[i] = worker;

                worker.Name     = threadName;
                worker.Priority = threadPriority;
                worker.Start();
            }
        }