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表格差不多,批量修改 ...
随机推荐
- Spire.Doc组件
使用Spire.Doc组件利用模板导出Word文档 以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. ...
- Enhancing network controls in mandatory access control computing environments
A Mandatory Access Control (MAC) aware firewall includes an extended rule set for MAC attributes, su ...
- TensorFlow 学习(六) —— TensorFlow 与 numpy 的交互
1. 将 numpy 下的多维数组(ndarray)转化为 tensor a = np.zeros((3, 3)) ta = tf.convert_to_tensor(a) with tf.Sessi ...
- C# 8.0 预览特性
初试C# 8.0 Visual Studio 2019的第一个预览版(使用Visual Studio 2019提高每个开发人员的工作效率)和.NET Core 3.0(宣布.NET Core 3预览1 ...
- 反编译Jar包
Jar 包(Java Archive)是对 Java 程序的打包,它可能包含源码,也可能没有. 对于有包含源码的 Jar 包,在 Eclipse 工程里设定好 source code 路径后能直接查看 ...
- Android 光标位置设置
EditText edit =(EditText) findViewById(R.id.etTest); 1.设置光标在EditText中的指定位置 edit.setSelection(1); 需要注 ...
- delphi的bpl、dcp 、dcu文件意义(BPL相当于C++中的DLL,DCP相当于C++中的Lib,编译时需要)
BPL 英文全称 Borland Package library ,是一种特殊的DLL文件,用于代码重用和减少可执行文件.编译bpl时,仅需要添加相应功能的pas文件,如果有窗体,则需要添加dfm文 ...
- 极简代码(八)—— binary activation function
二值化的激活函数: x > 1 ? 1 : -1; ⇒ [1, -1]; x = 0 ⇒ -1; 当然也可以使用sign() 函数(求符号函数): sign(x) % 但要注意的是,sign(0 ...
- QPointer,QSharedPointer,QWeakPointer的区别与使用例子(QSharedPointer类似Delphi里的引用计数,是强引用,而QWeakPointer是弱引用,不影响原始对象的引用计数,相当于是在暗中观察对象,但保持联系,需要的时候就会出现)
QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal ...
- Matlab Tricks(二十四)—— 将一副图像逆时针旋转 180°
function I2 = rot180(I) I2 = I(end:-1:1, end:-1:1); % 上下颠倒,左右颠倒: