在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 = -;
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>();
para.Add("strConfig", "");
para.Add("strTypeValue", ""); ThreadRunExt(GetSource, para, null, new Control[] { this.xtabMain });
  private void GetSource(Dictionary<string, object> para)
{.....}

C#WinForm线程基类的更多相关文章

  1. 工作线程基类TaskSvc

    工作线程基类TaskSvc 前端时间用ACE写代码,发ACE_Task确实好用.不但能提供数量一定的线程,还能够让这些继承的线程函数自由访问子类的private和protected变量.此外,ACE_ ...

  2. (一)c#Winform自定义控件-基类控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (十七)c#Winform自定义控件-基类窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. Winform框架中窗体基类的用户身份信息的缓存和提取

    在Winform开发中,有时候为了方便,需要把窗体的一些常规性的数据和操作函数进行封装,通过自定义基类窗体的方式,可以实现这些封装管理,让我们的框架统一化.简单化的处理一些常规性的操作,如这里介绍的用 ...

  5. 6、面向对象以及winform的简单运用(抽象基类与接口)

    抽象类与抽象方法 1.书写规范: 在类前面加上abstract关键字,就成为了抽象类:在一个方法前面加上abstract关键字,就成为了抽象方法(抽象方法不能有实现方法,直接在后面加分号) 例: ab ...

  6. winform中利用反射实现泛型数据访问对象基类(3)

    继续完善了几点代码 满足没有主键的情况下使用 并且完善实体字段反射设置value时的类型转换 /// <summary> /// DAO基类 实体名必须要与数据表字段名一致 /// < ...

  7. winform中利用反射实现泛型数据访问对象基类(2)

    在1的基础上做了一点改进 参数化处理 看上去更简洁 无主键情况下 update 方法需要改进 insert delete没有问题  /// <summary>     /// DAO基类 ...

  8. winform中利用反射实现泛型数据访问对象基类(1)

    考虑到软件使用在客户端,同时想简化代码的实现,就写了一个泛型的数据访问对象基类,并不是特别健全,按道理应该参数化的方式实现insert和update,暂未使用参数化,抽时间改进. /// <su ...

  9. C# Winform下一个热插拔的MIS/MRP/ERP框架15(窗体基类场景1)

    最基础的窗体基类其实是通过应用场景反推的结构. 以下是场景一: 单表应用,普通的数据,比如单位/颜色/特殊字典等使用者少的,无需过多控制的可以使用一个数据表格来管理. 和Excel表格差不多,批量修改 ...

随机推荐

  1. request.getSession().getServletContext().getRealPath()的一些坑

    今天是学校机房的服务器上对之前的一个网站升级时发现了一个bug,我自己的机器上用的tomcat8,机房上是tomcat7,结果一运行就开始报找不到文件,最后发现是文件分隔符的问题 原来在代码中涉及到路 ...

  2. redux相关学习资源

    很多学习资料,直接在SF.掘金搜索关键词redux源码等可以获得. redux参考版本3.6或3.7.2   redux-thunk看1.0.1 1.redux源码分析之四:compose函数    ...

  3. xaml 添加 region

    原文:xaml 添加 region 本文告诉大家如何在 xaml 添加 region 在 VisualStudio 2015 和 VisualStudio 2017 微软支持在 xmal 使用 reg ...

  4. WPF--3Dmax+blend+WPF综合运用

    引自:http://blog.sina.com.cn/s/blog_95dbdf9e0100we3z.html 本人小菜,WPF刚入门,只是写一下最近的项目心得.欢迎各位前辈们前来拍砖指正,感激不敬! ...

  5. FastDFS安装、配置、部署(三)-Storage配置具体解释

    1.基本配置 # is this config file disabled # false for enabled # true for disabled disabled=false # the n ...

  6. P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!

    如图,为什么这样取出来的p,p1的值不一样呢?   165232328群友庾伟洪告诉我:  P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址) ,你想P指向真正的对 ...

  7. BLAS 与 Intel MKL 数学库

    0. BLAS BLAS(Basic Linear Algebra Subprograms)描述和定义线性代数运算的规范(specification),而不是一种具体实现,对其的实现包括: AMD C ...

  8. 使用batch批处理做目录及流程选择

    @echo off rem -- http://phpnow.org rem -- YinzCN_at_Gmail.com setlocal enableextensions if exist Pn\ ...

  9. Windows下程序打包发布时的小技巧(使用Dependency Walker侦测不理想,改用VS自带的dumpbin则万无一失,还可查看dll导出的函数)

    Windows下开发的应用程序在发布时,需要将其依赖的一些动态链接库一起打进安装包里面去.这个时候,快速确定这个程序到底依赖哪些动态链接库变得非常重要.很久以前写过一篇关于Qt程序安装包制作的博客,里 ...

  10. rc-form(翻译)

    原地址:https://npm.taobao.org/package/rc-form rc-form React 高阶表单控制组件.       开发 npm install npm start op ...