/// <summary>
        ///     Creates a new action dispatcher task.
        /// </summary>
        /// <param name="action">The action to execute.</param>
        /// <param name="callback">The callback to run when this task has been executed.</param>
        internal static ActionDispatcherTask Create(Action action, ActionDispatchCompleteCallback callback)
        {
            ActionDispatcherTask task = ObjectCache.GetActionDispatcherTask();

            task.isCurrentlyLoungingInAPool = false;

            task.action   = action;
            task.callback = callback;

            return(task);
        }
Esempio n. 2
0
        /// <summary>
        ///     Queues the operation for execution on the main thread.
        /// </summary>
        /// <param name="action">The operation to execute.</param>
        /// <param name="callback">The callback to invoke once this is complete.</param>
        /// <returns>A DispatcherTask for this operation.</returns>
        /// <remarks>
        ///     This returns an IDisposable object, it is your responsibility to dispose of it when you're done!
        /// </remarks>
        /// <exception cref="DispatcherException">Thrown if an unhandled exception was raised while executing the dispatcher task when completing synchronously.</exception>
        public ActionDispatcherTask InvokeAsync(Action action, ActionDispatchCompleteCallback callback)
        {
            //Queue the operation and wait.
            ActionDispatcherTask task = ActionDispatcherTask.Create(action, callback);

            //Complete synchronously if already executor thread
            if (Thread.CurrentThread.ManagedThreadId == executorThreadID)
            {
                task.Execute(true);
            }
            else
            {
                lock (tasks)
                    tasks.Enqueue(task);

                jobMutex.Set();
            }

            return(task);
        }