在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. Service熟人

    Service 为一体的四个分量间(剩余有Activity ,内容提供商,广播),它属于后台工作,能够在后台很长一段时间执行,他没有接口. 首先从使用方式上来说来说 他有两种使用方式: 1.启动式使用 ...

  2. Vue挂载元素的替换

    Vue根组件已有挂载DOM'#app',在render又引进一个组件,该组件最外层也是用了'#app',为何根组件的DOM'#app'会被替换掉. //main.js import Vue from ...

  3. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. Dojo第一节:学会使用firebug对js,Dojo进行调适

    内容概要: 学会使用firebug的基本功能 1. 简介:Firebug是Firefox的一个插件,用来对js代码进行调适的工具. (官方废话:Firebug是firefox下的一个插件,可以调试全部 ...

  5. java基础篇---文件上传(组件)

    转载自:http://www.cnblogs.com/oumyye/p/4234969.html 文件上传几乎是所有网站都具有的功能,用户可以将文件上传到服务器的指定文件夹中,也可以保存在数据库中,本 ...

  6. 【Python注意事项】如何理解python中间generator functions和yield表情

    本篇记录自己的笔记Python的generator functions和yield理解表达式. 1. Generator Functions Python支持的generator functions语 ...

  7. windows 路径

    windows下的路径分隔符是\,而不是/ hosts文件的位置:C:\Windows\system32\drivers\etc 安卓(Android)用户:Android手机hosts文件路径:/s ...

  8. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  9. WinEdt && LaTex(四)—— 自定义新命令(newcommand、def)

    1. 新建命令 使用如下的命令:\newcommand{name}[num]{definition}: 该命令(newcommand)有两个参数,第一个 name 是你想要建立的命令的名称,第二个def ...

  10. [Songqw.Net 基础]WPF实现简单的插件化开发

    原文:[Songqw.Net 基础]WPF实现简单的插件化开发 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/ar ...