• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#操作Control异步工具类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/// <summary>
    /// 异步工具类
    /// </summary>
    public class TaskTools
    {
        /// <summary>
        /// 是否 在执行回调函数之前修改Running状态
        /// </summary>
        public bool ChangeRunningStateBeforeCallback { get; private set; }

        /// <summary>
        /// 是否 正在执行异步任务
        /// </summary>
        public bool Running { get; private set; }


        public TaskTools()
            : this(false)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="changeRunningStateBeforeCallback">是否 在执行回调函数之前修改Running状态 默认值false</param>
        public TaskTools(bool changeRunningStateBeforeCallback)
        {
            this.ChangeRunningStateBeforeCallback = changeRunningStateBeforeCallback;
        }

        /// <summary>
        /// 执行异步任务
        /// </summary>
        /// <typeparam name="T">异步任务返回值类型</typeparam>
        /// <param name="control">操作UI时需要Invoke的控件</param>
        /// <param name="asyncFunc">将要执行的任务任务</param>
        /// <param name="callback">异步任务执行完毕后执行的回调函数</param>
        public void Run<T>(Control control, Func<T> asyncFunc, Action<T> callback)
        {
            if (this.Running)
                throw new InvalidOperationException(" the task is running ");
            try
            {
                this.Running = true;
                Task<T> task = new Task<T>(() =>
                {
                    try
                    {
                        return asyncFunc();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return default(T);
                    }
                });

                task.Start();

                TaskContinue<T>(control, task, callback);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                this.Running = false;
            }
        }


        /// <summary>
        /// 执行异步任务
        /// </summary>
        /// <typeparam name="T">异步任务返回值类型</typeparam>
        /// <param name="control">操作UI时需要Invoke的控件</param>
        /// <param name="args">异步任务的传入参数</param>
        /// <param name="asyncFunc">将要执行的任务任务</param>
        /// <param name="callback">异步任务执行完毕后执行的回调函数</param>
        public void Run<T>(Control control, object args, Func<object, T> asyncFunc, Action<T> callback)
        {
            if (this.Running)
                throw new InvalidOperationException(" the task is running ");

            try
            {
                this.Running = true;
                Task<T> task = new Task<T>((lambdaObj) =>
                {
                    try
                    {
                        return asyncFunc(lambdaObj);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return default(T);
                    }
                }, args);

                task.Start();

                TaskContinue<T>(control, task, callback);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                this.Running = false;
            }
        }


        /// <summary>
        /// 延时执行某任务
        /// </summary>
        /// <param name="control">操作UI时需要Invoke的控件</param>
        /// <param name="milliSecond">将要延时执行的毫秒数</param>
        /// <param name="callback">异步任务执行完毕后执行的回调函数</param>
        public void DelayedRun(int milliSecond, Control control, Action callback)
        {
            this.Run<int>(control, () =>
            {
                Thread.Sleep(milliSecond); // 4.0 类库
                return milliSecond;
            }, (time) =>
            {
                callback();
            });
        }


        /// <summary>
        /// Control.Invoke方法的简易封装
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="control"></param>
        /// <param name="args"></param>
        /// <param name="action"></param>
        public static void ControlInvoke<T>(Control control, T args, Action<T> action)
        {
            try
            {
                Invoke<T>(control, args, action);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 异步任务完成后继续执行...
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <param name="task"></param>
        /// <param name="callback"></param>
        private void TaskContinue<T>(Control control, Task<T> task, Action<T> callback)
        {
            task.ContinueWith((lambdaAction) =>
            {
                if (this.ChangeRunningStateBeforeCallback)
                {
                    this.Running = false;
                }
                try
                {
                    if (callback != null)
                    {
                        // 有UI控件 则将回调函数 注入到UI控件的相关线程中去执行
                        if (control != null)
                        {
                            TaskTools.Invoke<T>(control, lambdaAction.Result, callback);
                        }
                        else
                        {
                            // 否则在当前线程内执行 回调函数
                            callback(lambdaAction.Result);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    this.Running = false;
                }
            });
        }

        /// <summary>
        /// Control.Invoke方法的简易封装
        /// 注意 无 Try Catch
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="control"></param>
        /// <param name="args"></param>
        /// <param name="action"></param>
        private static void Invoke<T>(Control control, T args, Action<T> action)
        {
            // control为空,在当前线程内执行该action
            if (control == null)
            {
                action(args);
                return;
            }

            // 控件正在释放或者已经被释放则不执行action
            if (control.Disposing || control.IsDisposed)
                return;

            if (control.InvokeRequired)
            {
                control.Invoke(action, new object[] { args });
            }
            else
            {
                action(args);
            }
        }
    }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap