欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#WinForm线程基类

程序员文章站 2022-06-10 18:34:07
...

转载自:http://www.bubuko.com/infodetail-2001031.html

在CS模式开发中一般我们需要用到大量的线程来处理比较耗时的操作,以防止界面假死带来不好的体验效果,下面我将我定义的线程基类给大家参考下

#region 方法有返回值
        /// <summary>
        /// 功能描述:多线程执行方法,方法有返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为object[],返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="enableControl">调用线程时,禁用的控件</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRun(Func<List<string>, object> func, List<string> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    object obj = func(objParams);
                    if (callback != null)
                        callback(obj);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }
        /// <summary>
        /// 功能描述:多线程执行方法,方法有返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为object[],返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRun(Func<List<object>, object> func, List<object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    object obj = func(objParams);
                    if (callback != null)
                        callback(obj);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }

        /// <summary>
        /// 功能描述:多线程执行方法,方法有返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为ictionary<string,object>,返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRun(Func<Dictionary<string, object>, object> func, Dictionary<string, object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    object obj = func(objParams);
                    if (callback != null)
                        callback(obj);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }

        /// <summary>
        /// 功能描述:多线程执行方法,方法无参数,有返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRun(Func<object> func, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    object obj = func();
                    if (callback != null)
                        callback(obj);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }
        #endregion

        #region 方法无返回值
        /// <summary>
        /// 功能描述:多线程执行方法,方法无返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为object[],如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRunExt(Action<List<string>> func, List<string> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    func(objParams);
                    if (callback != null)
                        callback(null);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }
        /// <summary>
        /// 功能描述:多线程执行方法,方法无返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为object[],如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRunExt(Action<List<object>> func, List<object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    func(objParams);
                    if (callback != null)
                        callback(null);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }
        /// <summary>
        /// 功能描述:多线程执行方法,方法无返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,参数为ictionary<string,object>,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRunExt(Action<Dictionary<string, object>> func, Dictionary<string, object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    func(objParams);
                    if (callback != null)
                        callback(null);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }

        /// <summary>
        /// 功能描述:多线程执行方法,方法无参数无返回值
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:44:26
        /// 任务编号:MES
        /// </summary>
        /// <param name="func">方法,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param>
        /// <param name="objParams">方法参数</param>
        /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param>
        /// <param name="enableControl">调用线程时禁用的控件</param>
        /// <param name="blnShowSplashScreen">是否显示提示层</param>
        /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param>
        protected void ThreadRunExt(Action func, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null)
        {
            if (blnShowSplashScreen)
            {
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("提示");
                if (string.IsNullOrEmpty(strMsg))
                    strMsg = "处理正在进行中,请稍候...";
                splashScreenManager1.SetWaitFormDescription(strMsg);
            }
            if (enableControl != null)
            {
                SetControlEnableds(enableControl, false);
            }
            Thread th = new Thread(delegate()
            {
                try
                {
                    func();
                    if (callback != null)
                        callback(null);
                }
                catch (Exception ex)
                {
                    if (callback != null)
                        callback(ex);
                    else
                        ThreadBaseCallBack(ex);
                }
                finally
                {
                    if (blnShowSplashScreen)
                    {
                        ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); });
                    }
                    if (enableControl != null)
                    {
                        ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); });
                    }
                }
            });
            th.IsBackground = true;
            th.Start();
        }
        #endregion

        /// <summary>
        /// 功能描述:委托调用,用于夸线程访问控件
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 17:58:53
        /// 任务编号:MES
        /// </summary>
        /// <param name="action">action</param>
        /// <param name="f">所在窗体,默认使用当前窗体</param>
        protected void ThreadInvokerControl(Action action, Form frm = null)
        {
            if (frm == null)
                frm = this;
            frm.BeginInvoke(action);
        }

        /// <summary>
        /// 功能描述:线程默认回调方法
        /// 作  者:huangzh
        /// 创建日期:2017-03-29 19:31:19
        /// 任务编号:MES
        /// </summary>
        /// <param name="obj">obj</param>
        private void ThreadBaseCallBack(object obj)
        {
            if (obj is Exception)
            {
                ThreadInvokerControl(() => { throw obj as Exception; });
            }
        }


        #region 禁用控件时不改变空间颜色
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        private const int GWL_STYLE = -16;
        private const int WS_DISABLED = 0x8000000;

        /// <summary>
        /// 功能描述:设置控件的Enable属性,控件不改颜色
        /// 作  者:huangzh
        /// 创建日期:2017-03-30 09:01:45
        /// 任务编号:MES
        /// </summary>
        /// <param name="c">c</param>
        /// <param name="enabled">enabled</param>
        public void SetControlEnabled(Control c, bool enabled)
        {
            if (enabled)
            {
                SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE));
            }
            else
            {
                SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE));
            }
        }
        /// <summary>
        /// 功能描述:设置多个控件的Enable属性,控件不改颜色
        /// 作  者:huangzh
        /// 创建日期:2017-03-30 09:07:12
        /// 任务编号:MES
        /// </summary>
        /// <param name="cs">cs</param>
        /// <param name="enabled">enabled</param>
        public void SetControlEnableds(Control[] cs, bool enabled)
        {
            foreach (var c in cs)
            {
                SetControlEnabled(c, enabled);
            }
        }
        #endregion


再看使用方法:

Dictionary<string, object> para = new Dictionary<string, object>();
2             para.Add("strConfig", "1");
3             para.Add("strTypeValue", "2");
4           
5             ThreadRunExt(GetSource, para, null, new Control[] { this.xtabMain });


private void GetSource(Dictionary<string, object> para)
2         {.....}







































相关标签: 线程