Example #1
0
        /// <summary>
        /// Creates a <see cref="ScheduledTask"/>.
        /// </summary>
        /// <param name="threadMode">The manner in which the scheduled task executes.</param>
        /// <param name="priority">The thread priority to assign if a dedicated thread is used. This is ignored if using the thread-pool.</param>
        /// <param name="disposeOnShutdown">Adds a handler to <see cref="ShutdownHandler"/> that requires this class to be disposed
        /// when the application is shutdown. Note: If this object has been garbage collected, this will have no effect.</param>
        public ScheduledTask(ThreadingMode threadMode = ThreadingMode.ThreadPool, ThreadPriority priority = ThreadPriority.Normal, bool disposeOnShutdown = false)
        {
            m_workerThreadID = -1;
            m_waitForDispose = new ManualResetEvent(false);
            m_disposeSync    = new object();

            switch (threadMode)
            {
            case ThreadingMode.DedicatedForeground:
                m_thread = new ThreadContainerDedicated(OnRunningCallback, Dispose, false, priority, disposeOnShutdown);
                break;

            case ThreadingMode.DedicatedBackground:
                m_thread = new ThreadContainerDedicated(OnRunningCallback, Dispose, true, priority, disposeOnShutdown);
                break;

            case ThreadingMode.ThreadPool:
                m_thread = new ThreadContainerThreadpool(OnRunningCallback, Dispose, disposeOnShutdown);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(threadMode));
            }
        }
Example #2
0
        /// <summary>
        /// Creates a <see cref="ScheduledTask"/>.
        /// </summary>
        /// <param name="threadMode">The manner in which the scheduled task executes.</param>
        /// <param name="priority">The thread priority to assign if a dedicated thread is used. This is ignored if using the thread-pool.</param>
        public ScheduledTask(ThreadingMode threadMode = ThreadingMode.ThreadPool, ThreadPriority priority = ThreadPriority.Normal)
        {
            m_workerThreadID = -1;
            m_waitForDispose = new ManualResetEvent(false);
            m_disposeSync    = new object();

            switch (threadMode)
            {
            case ThreadingMode.DedicatedForeground:
                m_thread = new ThreadContainerDedicated(new WeakAction <ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken), false, priority);
                break;

            case ThreadingMode.DedicatedBackground:
                m_thread = new ThreadContainerDedicated(new WeakAction <ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken), true, priority);
                break;

            case ThreadingMode.ThreadPool:
                m_thread = new ThreadContainerThreadpool(new WeakAction <ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken));
                break;

            default:
                throw new ArgumentOutOfRangeException("threadMode");
            }
        }
        /// <summary>
        /// Creates a <see cref="ScheduledTask"/>.
        /// </summary>
        /// <param name="threadMode">The manner in which the scheduled task executes.</param>
        /// <param name="priority">The thread priority to assign if a dedicated thread is used. This is ignored if using the thread-pool.</param>
        /// <param name="disposeOnShutdown">Adds a handler to <see cref="ShutdownHandler"/> that requires this class to be disposed
        /// when the application is shutdown. Note: If this object has been garbage collected, this will have no effect.</param>
        public ScheduledTask(ThreadingMode threadMode = ThreadingMode.ThreadPool, ThreadPriority priority = ThreadPriority.Normal, bool disposeOnShutdown = false)
        {
            m_workerThreadID = -1;
            m_waitForDispose = new ManualResetEvent(false);
            m_disposeSync = new object();

            switch (threadMode)
            {
                case ThreadingMode.DedicatedForeground:
                    m_thread = new ThreadContainerDedicated(OnRunningCallback, Dispose, false, priority, disposeOnShutdown);
                    break;
                case ThreadingMode.DedicatedBackground:
                    m_thread = new ThreadContainerDedicated(OnRunningCallback, Dispose, true, priority, disposeOnShutdown);
                    break;
                case ThreadingMode.ThreadPool:
                    m_thread = new ThreadContainerThreadpool(OnRunningCallback, Dispose, disposeOnShutdown);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(threadMode));
            }
        }
        // Executed by the worker thread
        private void OnRunningCallback(ThreadContainerBase.CallbackArgs args)
        {
            bool disposing = m_disposing;

            if (disposing && args.StartDisposalCallSuccessful)
            {
                args.ShouldDispose = true;
                TryCallback(ScheduledTaskRunningReason.Disposing);
                return;
            }

            TryCallback(ScheduledTaskRunningReason.Running);

            if (disposing)
            {
                args.ShouldDispose = true;
                TryCallback(ScheduledTaskRunningReason.Disposing);
            }
        }
Example #5
0
        /// <summary>
        /// Creates a <see cref="ScheduledTask"/>.
        /// </summary>
        /// <param name="threadMode">The manner in which the scheduled task executes.</param>
        /// <param name="priority">The thread priority to assign if a dedicated thread is used. This is ignored if using the thread-pool.</param>
        public ScheduledTask(ThreadingMode threadMode = ThreadingMode.ThreadPool, ThreadPriority priority = ThreadPriority.Normal)
        {
            m_workerThreadID = -1;
            m_waitForDispose = new ManualResetEvent(false);
            m_disposeSync = new object();

            switch (threadMode)
            {
                case ThreadingMode.DedicatedForeground:
                    m_thread = new ThreadContainerDedicated(new WeakAction<ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken), false, priority);
                    break;
                case ThreadingMode.DedicatedBackground:
                    m_thread = new ThreadContainerDedicated(new WeakAction<ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken), true, priority);
                    break;
                case ThreadingMode.ThreadPool:
                    m_thread = new ThreadContainerThreadpool(new WeakAction<ThreadContainerBase.CallbackArgs>(OnRunningCallback, out m_weakCallbackToken));
                    break;
                default:
                    throw new ArgumentOutOfRangeException("threadMode");
            }
        }