示例#1
0
        /// <summary>
        /// 以指定的时间 异步 执行任务, 时间到达后, 完成或超时则返回结果(任务停止执行)
        /// </summary>
        /// <typeparam name="EveryType">返回结果类型</typeparam>
        /// <param name="taskAction">待执行的任务</param>
        /// <param name="paramsObj">任务的参数</param>
        /// <param name="timeSpan">时间间隔</param>
        /// <param name="callbackAction">执行完成后的回调方法 返回参数为是否执行成功</param>
        /// <param name="throwException">超时是否抛出异常</param>
        /// <returns></returns>
        public static async Task RunThreadWithTimeoutAsync(Action taskAction, TimeSpan timeSpan, Action <Boolean> callbackAction, Boolean throwException)
        {
            Task <Boolean> backgroundTask = Task.Factory.StartNew <Boolean>(() => { return(FunctionHelper.RunThreadWithTimeout(taskAction, timeSpan, throwException)); });
            Boolean        isCompleted    = await backgroundTask.ConfigureAwait(false);

            callbackAction.Invoke(isCompleted);
        }
示例#2
0
        /// <summary>
        /// 以指定的时间 异步 执行有一个参数且有返回值的任务, 时间到达后, 完成或超时则返回结果(任务停止执行)
        /// </summary>
        /// <typeparam name="EveryType">返回结果类型</typeparam>
        /// <param name="taskAction">待执行的任务</param>
        /// <param name="paramsObj">任务的参数</param>
        /// <param name="timeSpan">时间间隔</param>
        /// <param name="callbackAction">执行完成后的回调方法 返回参数为是否执行成功</param>
        /// <param name="throwException">超时是否抛出异常</param>
        /// <returns></returns>
        public static async Task <EveryType> RunThreadWithTimeoutAsync <EveryType>(Func <Object, EveryType> taskAction, Object paramsObj, TimeSpan timeSpan, Action <Boolean> callbackAction, Boolean throwException)
        {
            Boolean          isCompleted    = false;
            Task <EveryType> backgroundTask = Task.Factory.StartNew <EveryType>(() => { return(FunctionHelper.RunThreadWithTimeout(taskAction, paramsObj, timeSpan, throwException, out isCompleted)); });
            EveryType        everyType      = await backgroundTask.ConfigureAwait(false);

            callbackAction.Invoke(isCompleted);
            return(everyType);
        }