Пример #1
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);
        }
Пример #2
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);
        }