Example #1
0
        /// <summary>
        /// Creates an operation that will complete or timeout.  The delegate passed in is required
        /// and is will be called when the <see cref="Action"/> completes.
        /// </summary>
        /// <exception cref="ArgumentNullException"><para>The argument <paramref name="executingActionWithCompletionCallback"/> is <see langword="null"/>.</para></exception>
        /// <param name="executingActionWithCompletionCallback">The execute action with completion callback.</param>
        /// <param name="timeout">The timeout.</param>
        /// <returns>
        /// <para>An <see cref="ExecuteAsyncOperation"/>, which you need to <c>yield return</c>.  This value contains asynchronous state information about the dispatched task.
        /// Execution will resume at the statement following the <c>yield return</c> after the action has completed or a timeout occurs.</para>
        /// </returns>
        public ExecuteAsyncOperation Create(Action <Action> executingActionWithCompletionCallback, TimeSpan timeout)
        {
            if (executingActionWithCompletionCallback == null)
            {
                throw new ArgumentNullException("executingActionWithCompletionCallback");
            }

            var operation = new ExecuteAsyncOperation {
                Timeout = timeout
            };

            executingActionWithCompletionCallback(
                () =>
            {
                // this callback will be called by executeActionWithCompletionCallback()
                // when it has finished.
                try
                {
                    operation.Set();                              // this will prevent timeout, and call the competion method to get executed.
                }
                catch (Exception e)
                {
                    HandleCaughtException(e);
                }
            });

            return(operation);
        }
Example #2
0
        /// <summary>
        /// Creates an operation that will complete or timeout.  The delegate passed in is required
        /// and is will be called when the Action completes.
        /// </summary>
        /// <param name="timeout">The timeout.</param>
        /// <returns>
        /// <para>An <see cref="ExecuteAsyncOperation"/>, which you need to "yield return".  This value contains asynchronous state information about the dispatched task.
        /// Execution will resume at the statement following the "yield return" after the action has completed or a timeout occurs.</para>
        /// </returns>
        public ExecuteAsyncOperation Create(TimeSpan timeout)
        {
            var operation = new ExecuteAsyncOperation {
                Timeout = timeout
            };

            return(operation);
        }