示例#1
0
        private void ThreadMain()
        {
            while (!this._isDisposed)
            {
                if (IsActive)
                {
                    // The thread is waking up.
                    IsSleeping = false;

                    Job nextJob      = null;
                    int jobsLeftToDo = 0;

                    // Gets the next job, if any.
                    lock (this._syncRoot)
                    {
                        // If the instance just got disposed, return immediately.
                        if (this._isDisposed)
                        {
                            return;
                        }

                        // Gets the next job, or null if none is found.
                        nextJob = this._jobQueue.Dequeue();
                    }

                    // If there is a job to execute, do it.
                    if (nextJob != null)
                    {
                        nextJob.Action();
                    }

                    // Gets how many jobs are still in queue.
                    lock (_syncRoot)
                    {
                        jobsLeftToDo = this._jobQueue.Count;
                    }

                    // If there are more jobs to do and we should do them, let's go!
                    if (jobsLeftToDo > 0 && ContinuesOnCompletion)
                    {
                        // Sleeps a litte bit before to free UI thread cpu.
                        Thread.Sleep(DelayBetweenJobs);

                        // Let's do the next job!
                        continue;
                    }

                    // Goes to sleep and waits for a signal.
                    IsSleeping = true;
                }

                // Zzzz.
                this._jobThreadCanResumeEvent.WaitOne();
            }
        }
示例#2
0
        /// <summary>
        /// Runs continuations pushed on the loop.
        /// </summary>
        public void RunJobs()
        {
            Job job = null;

            while (job != null || _queue.Count > 0)
            {
                if (job == null)
                {
                    lock (_queue)
                    {
                        job = _queue.Dequeue();
                    }
                }

                if (job.Priority < DispatcherPriority.Input && s_platform.HasMessages())
                {
                    break;
                }

                if (job.TaskCompletionSource == null)
                {
                    job.Action();
                }
                else
                {
                    try
                    {
                        job.Action();
                        job.TaskCompletionSource.SetResult(null);
                    }
                    catch (Exception e)
                    {
                        job.TaskCompletionSource.SetException(e);
                    }
                }

                job = null;
            }
        }
示例#3
0
        private void DoJob(object jobWrapper)
        {
            Job job = (Job)jobWrapper;

            try
            {
                job.Action(job.Argument);
                ReportActionFinished();
                return;
            }
            catch (Exception)
            {
                ReportActionFailed();
                //Console.WriteLine(e);
                throw;
            }
        }
示例#4
0
        public void Run(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                Job job = null;

                while (job != null || this.queue.Count > 0)
                {
                    if (job == null)
                    {
                        lock (this.queue)
                        {
                            job = this.queue.Dequeue();
                        }
                    }

                    if (job.Priority < DispatcherPriority.Input && platform.HasMessages())
                    {
                        break;
                    }

                    try
                    {
                        job.Action();
                        job.TaskCompletionSource.SetResult(null);
                    }
                    catch (Exception e)
                    {
                        job.TaskCompletionSource.SetException(e);
                    }

                    job = null;
                }

                platform.ProcessMessage();
            }
        }