Ejemplo n.º 1
0
        /// <summary>
        /// Invokes an action in the GUI thread after the provided time span
        /// </summary>
        /// <returns>
        /// A timer object
        /// </returns>
        /// <param name='action'>
        /// The action to execute.
        /// </param>
        /// <remarks>
        /// This method schedules the execution of the provided function. The function
        /// must return 'true' if it has to be executed again after the time span, or 'false'
        /// if the timer can be discarded.
        /// The execution of the funciton can be canceled by disposing the returned object.
        /// </remarks>
        public static IDisposable TimeoutInvoke(TimeSpan timeSpan, Func <bool> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (timeSpan.Ticks < 0)
            {
                throw new ArgumentException("timeSpan can't be negative");
            }

            Timer t = new Timer();

            t.Id = engine.TimerInvoke(delegate {
                bool res = false;
                try {
                    toolkit.EnterUserCode();
                    res = action();
                    toolkit.ExitUserCode(null);
                } catch (Exception ex) {
                    toolkit.ExitUserCode(ex);
                }
                return(res);
            }, timeSpan);
            return(t);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Invokes an action in the GUI thread after the provided time span
        /// </summary>
        /// <returns>
        /// A timer object
        /// </returns>
        /// <param name='action'>
        /// The action to execute.
        /// </param>
        /// <remarks>
        /// This method schedules the execution of the provided function. The function
        /// must return 'true' if it has to be executed again after the time span, or 'false'
        /// if the timer can be discarded.
        /// The execution of the funciton can be canceled by disposing the returned object.
        /// </remarks>
        public static IDisposable TimeoutInvoke(TimeSpan timeSpan, Func <bool> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            if (timeSpan.Ticks < 0)
            {
                throw new ArgumentException("timeSpan can't be negative");
            }

            // Capture the current toolkit. It will be used in the invocation
            var targetToolkit = Toolkit.CurrentEngine;

            Timer t = new Timer();

            t.Id = engine.TimerInvoke(delegate {
                return(targetToolkit.Invoke(action));
            }, timeSpan);
            return(t);
        }