示例#1
0
 /// <summary>
 /// Add task to running cache.
 /// </summary>
 /// <param name="task"></param>
 /// <param name="attachThread"></param>
 private void AddRunning(IPoolTask task, Task attachThread)
 {
     runningLocker.EnterWriteLock();
     task.AttachThread(attachThread);
     running.Add(task.Key, task);
     runningLocker.ExitWriteLock();
 }
示例#2
0
        private TaskPoolState currentState; // current state of task box.


        /// <summary>
        /// Add a task to Task Box.
        /// <para></para>
        /// Need to call <see cref="Resume"/> to run this task immediately.
        /// </summary>
        /// <param name="task"></param>
        public void AddTask(IPoolTask task)
        {
            locker.EnterWriteLock();
            remains.Enqueue(task);
            locker.ExitWriteLock();
            //run.Set();
        }
        public IPoolTask FetchNewTask()
        {
            lock (locker)
            {
                willFetch = true;
            }
            run.Set();

            IPoolTask current;

            while (true)
            {
                lock (locker)
                {
                    if (isFetched)
                    {
                        current   = task;
                        task      = null;
                        isFetched = false;
                        break;
                    }
                }
                Thread.Sleep(10);
            }

            return(current);
        }
示例#4
0
        /// <summary>
        /// Remove task from running cache.
        /// </summary>
        /// <param name="task"></param>
        private void RemoveRunning(IPoolTask task, bool postRun = true)
        {
            if (!isPostRunOne && postRun)
            {
                task.PostRun();
            }

            runningLocker.EnterWriteLock();
            if (isPostRunOne && postRun)
            {
                task.PostRun();
            }
            running.Remove(task.Key);
            runningLocker.ExitWriteLock();
        }
        public void Run()
        {
            while (true)
            {
                run.WaitOne();

                lock (locker)
                {
                    if (willFetch)
                    {
                        task = taskGenerator.Generate();
                        if (task != null)
                        {
                            task.PreRun();
                        }
                        willFetch = false;
                        isFetched = true;
                    }
                }
            }
        }
示例#6
0
        /// <summary>
        /// Add a thread to run tasks.
        /// </summary>
        /// <returns></returns>
        private bool AddThread()
        {
            int       count = 0;
            IPoolTask task  = FetchTask();

            if (task == null)
            {
                return(false);
            }

            Task thread = null;

            TaskPoolState m_toState;

            thread = new Task(() =>
            {
                while (true)
                {
                    if (task != null)
                    {
                        locker.EnterReadLock();
                        m_toState = toState;
                        locker.ExitReadLock();

                        if (m_toState == TaskPoolState.RUN)
                        {
                            task.Run();
                            RemoveRunning(task);
                            task = null;

                            /// When finished a task, check <see cref="toState"/>.
                            locker.EnterReadLock();
                            m_toState = toState;
                            locker.ExitReadLock();
                            if (m_toState == TaskPoolState.RUN)
                            {
                                task = FetchTask();
                                if (task != null)
                                {
                                    AddRunning(task, thread);
                                }
                            }
                        }
                        else
                        {
                            task.Cancel();
                            RemoveRunning(task, false);
                            task = null;
                        }
                    }

                    // When the intent is not to run, then cancel current task,
                    if (task == null)
                    {
                        runningLocker.EnterReadLock();
                        count = running.Count;
                        runningLocker.ExitReadLock();
                        if (count == 0)
                        {
                            locker.EnterWriteLock();
                            currentState = TaskPoolState.PAUSE;
                            locker.ExitWriteLock();
                        }
                        break;
                    }
                }
            });
            AddRunning(task, thread);
            thread.Start();
            return(true);
        }
示例#7
0
        /// <summary>
        /// Create a thread, and fetch a task to run in it.
        /// </summary>
        /// <returns></returns>
        private bool AddThread()
        {
            if (toState != TaskPoolState.RUN)
            {
                return(false);
            }

            IPoolTask task = taskFetch.FetchNewTask();

            if (task == null)
            {
                return(false);
            }

            Task thread = null;

            thread = new Task(() =>
            {
                while (true)
                {
                    task.Run();

                    /// When current task is finished, remove it from <see cref="running"/>.
                    locker.EnterWriteLock();
                    running.Remove(task.Key);
                    locker.ExitWriteLock();

                    /// If pool will run, then fetch a new task and add it to <see cref="running"/>
                    if (toState == TaskPoolState.RUN)
                    {
                        task = taskFetch.FetchNewTask();
                        if (task != null)
                        {
                            locker.EnterWriteLock();
                            task.AttachThread(thread);
                            running.Add(task.Key, task);
                            locker.ExitWriteLock();
                        }
                    }
                    else
                    {
                        task = null;
                    }

                    /// When this thread are running to end, get count of running tasks.
                    /// If count equals 0, it means there is no running task.
                    /// Then set current state to <see cref="TaskPoolState.PAUSE"/>
                    if (task == null)
                    {
                        int count = 0;
                        locker.EnterUpgradeableReadLock();
                        count = running.Count;
                        if (count == 0)
                        {
                            locker.EnterWriteLock();
                            currentState = TaskPoolState.PAUSE;
                            locker.ExitWriteLock();
                        }
                        locker.ExitUpgradeableReadLock();
                        break;
                    }
                }
            });
            task.AttachThread(thread);

            locker.EnterWriteLock();
            running.Add(task.Key, task);
            locker.ExitWriteLock();
            thread.Start();
            return(true);
        }