C#WinForm线程基类
在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线程基类的更多相关文章
- 工作线程基类TaskSvc
工作线程基类TaskSvc 前端时间用ACE写代码,发ACE_Task确实好用.不但能提供数量一定的线程,还能够让这些继承的线程函数自由访问子类的private和protected变量.此外,ACE_ ...
- (一)c#Winform自定义控件-基类控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十七)c#Winform自定义控件-基类窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- Winform框架中窗体基类的用户身份信息的缓存和提取
在Winform开发中,有时候为了方便,需要把窗体的一些常规性的数据和操作函数进行封装,通过自定义基类窗体的方式,可以实现这些封装管理,让我们的框架统一化.简单化的处理一些常规性的操作,如这里介绍的用 ...
- 6、面向对象以及winform的简单运用(抽象基类与接口)
抽象类与抽象方法 1.书写规范: 在类前面加上abstract关键字,就成为了抽象类:在一个方法前面加上abstract关键字,就成为了抽象方法(抽象方法不能有实现方法,直接在后面加分号) 例: ab ...
- winform中利用反射实现泛型数据访问对象基类(3)
继续完善了几点代码 满足没有主键的情况下使用 并且完善实体字段反射设置value时的类型转换 /// <summary> /// DAO基类 实体名必须要与数据表字段名一致 /// < ...
- winform中利用反射实现泛型数据访问对象基类(2)
在1的基础上做了一点改进 参数化处理 看上去更简洁 无主键情况下 update 方法需要改进 insert delete没有问题 /// <summary> /// DAO基类 ...
- winform中利用反射实现泛型数据访问对象基类(1)
考虑到软件使用在客户端,同时想简化代码的实现,就写了一个泛型的数据访问对象基类,并不是特别健全,按道理应该参数化的方式实现insert和update,暂未使用参数化,抽时间改进. /// <su ...
- C# Winform下一个热插拔的MIS/MRP/ERP框架15(窗体基类场景1)
最基础的窗体基类其实是通过应用场景反推的结构. 以下是场景一: 单表应用,普通的数据,比如单位/颜色/特殊字典等使用者少的,无需过多控制的可以使用一个数据表格来管理. 和Excel表格差不多,批量修改 ...
随机推荐
- cxf maven 依赖 No message body writer foud.. contenttype:application/json
最近使用cxf发布rest服务时遇到了如题的异常,最后发现是缺少依赖的问题.ps:我并没有使用spring cxf基本运行需要如下依赖 1 <dependency> <groupId ...
- spring-boot-sample-web-jsp
Pom文件需要配置的: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl&l ...
- PCI GXL学习之二次开发篇
作者:朱金灿 来源:http://blog.csdn.net/clever101 gxl的二次开发分初级和高级之分.初级是gxl提供了几百个模块供你编排成不同的作业.高级就是你可以编写你的算法模块,然 ...
- 「两」创建一个带 ssh 镜座服务(修订版)--采用 Dockerfile 创
创建目录 首先,创建一个叫做 sshd_ubuntu 的目录,用于存放我们的 Dockerfile .脚本文件.以及其它文件. $ mkdir sshd_ubuntu $ ls sshd_ubuntu ...
- VS编译环境中TBB配置和C++中lambda表达式
TBB(Thread Building Blocks),线程构建模块,是由Intel公司开发的并行编程开发工具,提供了对Windows,Linux和OSX平台的支持. TBB for Windows ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- WPF 画线动画效果实现
原文:WPF 画线动画效果实现 弄了将近三天才搞定的,真是艰辛的实现. 看了很多博客,都太高深了,而且想要实现的功能都太强大了,结果基础部分一直实现不了,郁闷啊~ 千辛万苦终于找到了一个Demo,打开 ...
- nginx配置http跳转https
配置相当简单,在配置文件头部加一行,如下: server { listen *:;//监听80端口 https://www.chenruhui.com$request_uri;//需要跳转的网页 } ...
- C类型和存储方式的语言变量
变量的作用域:有效射程变量 >局部变量 1.在函数中的局部变量,量在该函数结束后自己主动撤销 2.函数内的复合语句中的局部变量(比方for语句),这个变量在复合语句结束后自己主动撤销.这个变量能 ...
- GTK+浅谈之一Windows10下QtCreator中GTK+环境搭建(十几篇)
一.简介 虽然GTK+是Linux下的开发环境,因为其跨平台特性,有时候需要在Windows上用到它的.如下是在Windows10下配置GTK+的开发环境. Gnome的开发基础结构是围 ...