private static IAsyncResult WrapWithTimeoutInternal <TResult>(AsyncCallback callback, object state, BeginInvokeCallback beginCallback, AsyncCallback <TResult> endCallback, int timeout, object tag)
        {
            AsyncResultWrapperImpl <TResult> wrapper = new AsyncResultWrapperImpl <TResult>()
            {
                Callback      = callback,
                AsyncState    = state,
                BeginCallback = beginCallback,
                EndCallback   = endCallback,
                Tag           = tag,
                Timeout       = timeout
            };

            wrapper.ExecuteBeginDelegate();
            return(wrapper);
        }
        public static TResult UnwrapAndContinue <TResult>(IAsyncResult asyncResult, object tag)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            AsyncResultWrapperImpl <TResult> wrapper = asyncResult as AsyncResultWrapperImpl <TResult>;

            if (wrapper == null || wrapper.Tag != tag)
            {
                throw new ArgumentException(MvcResources.AsyncCommon_InvalidAsyncResult, "asyncResult");
            }

            return(wrapper.ExecuteEndDelegateOnce());
        }