在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. 从研究try catch 捕获不到异步错误谈谈学习方法的问题

    先看下面的代码,思考一下输出 try { throw new Error(3) } catch (e) { console.log(e) } try { setTimeout(function () ...

  2. KDE 邀请用户测试 Plasma Mobile 的首个专用 ISO 镜像(可以考虑做一个极客。。。)

    KDE 项目依旧在继续改进智能手机.平板电脑和其他移动设备的 Plasma Mobile 用户界面,并于近日发布了一个 ISO 镜像,邀请社区的尝鲜用户进行测试. 他们曾承诺在 2018 年年底之前, ...

  3. myeclipse codelive插件关闭

    开启这个插件时,当你运行web工程,打开页面会造成一些显示的问题,原因是codelive插件向你的页面代码中注入了一些js代码大概是如下这些 <script>"undefined ...

  4. 关于JDBC连接数据库时出现的Public Key Retrieval is not allowed错误

    问题描述 最近在学习MyBatis框架,参考官方的文档通过配置文件的方式已经实现了通过Configuration配置文件和mapper映射文件访问mysql8数据库,于是想试试不使用XML文件去构建S ...

  5. Jdbc连接MySQL 8时报错“MySQLNonTransientConnectionException: Public Key Retrieval is not allowed”

    一.问题 因停电检修,今天重启服务器后,再启动jboss就报错"MySQLNonTransientConnectionException: Public Key Retrieval is n ...

  6. 用Github账号领Ripple币

    最近随着比特币在互联网上的流行,其他各种电子货币也都增加了曝光率. 昨晚在 v2ex 上看到有人发帖,用 20RMB 换取 2013 年 5 月 1 日前使用过的 github 账号得到的一个验证码. ...

  7. Hexo里如何添加广告

    前期先用Hexo做个人网站,模板可以用https://github.com/828768/maupassant-hexo,关于如何加入广告,可以看一下https://sobaigu.com/hexo- ...

  8. cordova/phoneGap 开发调试工具

    原文地址 一.前言 Ripple Emulate:使用Google模拟器,可以帮到大部分的调试,除了需要调用手机设备方面的功能除外.GapDebug:真机安装apk,电脑端和真机端同步调试,适用的项目 ...

  9. new与属性访问的顺序,从一道JS面试题说起

    这段时间一直在研究设计模式,在看工厂模式的时候,看到一段代码 VehicleFactory.prototype.createVehicle = function ( options ) { if( o ...

  10. jQuery整体架构

    (function(global, factory) { factory(global); }(typeof window !== "undefined" ? window : t ...