C#多线程异步处理数据通用界面窗体(frmThreadOperating)
C#多线程异步处理数据通用界面窗体(frmThreadOperating)
2.查询完成
frmThreadOperating界面简介
设计了一个异步多线程处理数据的通用界面(C#异步操作等待窗体)frmThreadOperating,用于查询大数据、批量处理大数据时提高用户体验。
比如要实现大批量数据跨业务系统推送,如下图所示:
点击【查询】按钮开始分析数据并返回查询结果:
点击【批量共享】或【反共享】按钮开始批量共享数据:
【查询】按钮事件:
C# Code:
private void btnQuery_Click(object sender, EventArgs e)
{
if (txtOrgCode.Text + txt_DocType.Text + txtDocNo.Text + txt_D1.Text + txt_D2.Text == "")
{
Msg.Warning("数据量很大,必须指定查询条件!");
return;
}
QueryFMSData4Batch parameters = new QueryFMSData4Batch
{
orgCode = txtOrgCode.EditValue.ToStringEx(),
docTypes = ConvertEx.ToString(txt_DocType.EditValue),
docNo = txtDocNo.Text,
dateTime1 = ConvertEx.ToSqlDateTime(txt_D1.DateTime),
dateTime2 = ConvertEx.ToSqlDateTime(txt_D2.DateTime),
operate = "全部"
};
SetBatchButton(false);
CCursor.ShowWaitCursor();
frmThreadOperating.ShowMe(this, this.ToQuery, parameters, this.ToQueryAborting, this.ToQueryFinishied, "查询数据");
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
if (txtOrgCode.Text + txt_DocType.Text + txtDocNo.Text + txt_D1.Text + txt_D2.Text == "")
{
Msg.Warning("数据量很大,必须指定查询条件!");
return;
}
QueryFMSData4Batch parameters = new QueryFMSData4Batch
{
orgCode = txtOrgCode.EditValue.ToStringEx(),
docTypes = ConvertEx.ToString(txt_DocType.EditValue),
docNo = txtDocNo.Text,
dateTime1 = ConvertEx.ToSqlDateTime(txt_D1.DateTime),
dateTime2 = ConvertEx.ToSqlDateTime(txt_D2.DateTime),
operate = "全部"
};
SetBatchButton(false);
CCursor.ShowWaitCursor();
frmThreadOperating.ShowMe(this, this.ToQuery, parameters, this.ToQueryAborting, this.ToQueryFinishied, "查询数据");
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
frmThreadOperation窗体的ShowMe方法提供6个参数,其中有3个参数是事件委托:
C# Code:
/// <summary>
/// 显示线程操作窗体
/// </summary>
/// <param name="owner">父级窗体,打开线程操作窗体</param>
/// <param name="doSomething">执行操作</param>
/// <param name="parameters">传入参数</param>
/// <param name="aborting">取消线程通知</param>
/// <param name="finished">线程完成后回调事件</param>
/// <param name="windowCaption">设置窗体标题</param>
public static void ShowMe(
Form owner,
MyThreadExecuting doSomething, object parameters,
MyThreadAborting aborting,
MyThreadFinished finished,
string windowCaption = "")
{
if (_Instance == null)
{
_Owner = owner;
_OnOperating = doSomething;
_Params = parameters;
_OnFinished = finished;
_OnAborting = aborting;
_Instance = new frmThreadOperating();
_Instance.Text = windowCaption.IsNullOrEmpty() ? "异步操作等待窗体" : windowCaption;
_Instance.ShowDialog(owner);
}
else
{
Msg.Warning("异步任务正在运行中,请完成此任务后再操作!");
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 显示线程操作窗体
/// </summary>
/// <param name="owner">父级窗体,打开线程操作窗体</param>
/// <param name="doSomething">执行操作</param>
/// <param name="parameters">传入参数</param>
/// <param name="aborting">取消线程通知</param>
/// <param name="finished">线程完成后回调事件</param>
/// <param name="windowCaption">设置窗体标题</param>
public static void ShowMe(
Form owner,
MyThreadExecuting doSomething, object parameters,
MyThreadAborting aborting,
MyThreadFinished finished,
string windowCaption = "")
{
if (_Instance == null)
{
_Owner = owner;
_OnOperating = doSomething;
_Params = parameters;
_OnFinished = finished;
_OnAborting = aborting;
_Instance = new frmThreadOperating();
_Instance.Text = windowCaption.IsNullOrEmpty() ? "异步操作等待窗体" : windowCaption;
_Instance.ShowDialog(owner);
}
else
{
Msg.Warning("异步任务正在运行中,请完成此任务后再操作!");
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
3个事件委托类型:
C# Code:
#region 自定义委托类型
public delegate object MyThreadExecuting(object param);//线程主方法
public delegate void MyThreadFinished(object data);//主方法调用结束后,通知完成事件
public delegate bool MyThreadAborting();//线程终止事件,必须返回True;
#endregion
//来源:C/S框架网 | www.csframework.com | QQ:23404761
public delegate object MyThreadExecuting(object param);//线程主方法
public delegate void MyThreadFinished(object data);//主方法调用结束后,通知完成事件
public delegate bool MyThreadAborting();//线程终止事件,必须返回True;
#endregion
//来源:C/S框架网 | www.csframework.com | QQ:23404761
【查询】按钮事件调用ShowMe方法,对应的3个委托事件:
1.查询数据
C# Code:
private object ToQuery(object param)
{
//查询数据,返回DataTable
return FMSHelper.QueryAllDocs(param as QueryFMSData4Batch);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
//查询数据,返回DataTable
return FMSHelper.QueryAllDocs(param as QueryFMSData4Batch);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
2.查询完成
C# Code:
private void ToQueryFinishied(object data)
{
//查询数据完成,返回DataTable对象,绑定表格数据源
if (data != null)
{
gcSummary.DataSource = data;
_ColumnHeaderCheckBox.Detach();
_ColumnHeaderCheckBox = new ColumnHeaderCheckBox(gvSummary, colFlagCheck);
}
SetBatchButton(true);
CCursor.ShowDefaultCursor();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
//查询数据完成,返回DataTable对象,绑定表格数据源
if (data != null)
{
gcSummary.DataSource = data;
_ColumnHeaderCheckBox.Detach();
_ColumnHeaderCheckBox = new ColumnHeaderCheckBox(gvSummary, colFlagCheck);
}
SetBatchButton(true);
CCursor.ShowDefaultCursor();
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
3.查询时取消线程
C# Code:
private bool ToQueryAborting()
{
//允许查询的时候中断线程
return true;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
//允许查询的时候中断线程
return true;
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
frmThreadOperation窗体部分代码:
C# Code:
/// <summary>
/// 异步线程处理通用界面(异步操作等待窗体)
/// </summary>
public partial class frmThreadOperating : frmBase
{
#region 自定义委托类型
public delegate object MyThreadExecuting(object param);
public delegate void MyThreadFinished(object data);
public delegate bool MyThreadAborting();//线程终止事件,必须返回True;
public delegate void MyThreadClose();
#endregion
private static frmThreadOperating _Instance = null;
private static Thread _T;
private static Form _Owner;
private static Object _Params;//MyThreadExecuting事件的参数
private static MyThreadExecuting _OnOperating;
private static MyThreadFinished _OnFinished;
private static MyThreadAborting _OnAborting;
//计时器读秒
private int _Secondes = 0;
private frmThreadOperating()
{
InitializeComponent();
}
private void frmThreadOperating_Load(object sender, EventArgs e)
{
//开始计时
_Secondes = 1;
timer1.Start();
//启动线程
_T = new Thread(OnThreadStart);
_T.Start();
}
/// <summary>
/// 显示线程操作窗体
/// </summary>
/// <param name="owner">父级窗体,打开线程操作窗体</param>
/// <param name="doSomething">执行操作</param>
/// <param name="parameters">传入参数</param>
/// <param name="finished">线程完成后回调事件</param>
/// <param name="windowCaption">设置窗体标题</param>
public static void ShowMe(
Form owner,
MyThreadExecuting doSomething, object parameters,
MyThreadAborting aborting,
MyThreadFinished finished,
string windowCaption = "")
{
if (_Instance == null)
{
_Owner = owner;
_OnOperating = doSomething;
_Params = parameters;
_OnFinished = finished;
_OnAborting = aborting;
_Instance = new frmThreadOperating();
_Instance.Text = windowCaption.IsNullOrEmpty() ? "异步操作等待窗体" : windowCaption;
_Instance.ShowDialog(owner);
}
else
{
Msg.Warning("异步任务正在运行中,请完成此任务后再操作!");
}
}
//
//省略代码....
//
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 异步线程处理通用界面(异步操作等待窗体)
/// </summary>
public partial class frmThreadOperating : frmBase
{
#region 自定义委托类型
public delegate object MyThreadExecuting(object param);
public delegate void MyThreadFinished(object data);
public delegate bool MyThreadAborting();//线程终止事件,必须返回True;
public delegate void MyThreadClose();
#endregion
private static frmThreadOperating _Instance = null;
private static Thread _T;
private static Form _Owner;
private static Object _Params;//MyThreadExecuting事件的参数
private static MyThreadExecuting _OnOperating;
private static MyThreadFinished _OnFinished;
private static MyThreadAborting _OnAborting;
//计时器读秒
private int _Secondes = 0;
private frmThreadOperating()
{
InitializeComponent();
}
private void frmThreadOperating_Load(object sender, EventArgs e)
{
//开始计时
_Secondes = 1;
timer1.Start();
//启动线程
_T = new Thread(OnThreadStart);
_T.Start();
}
/// <summary>
/// 显示线程操作窗体
/// </summary>
/// <param name="owner">父级窗体,打开线程操作窗体</param>
/// <param name="doSomething">执行操作</param>
/// <param name="parameters">传入参数</param>
/// <param name="finished">线程完成后回调事件</param>
/// <param name="windowCaption">设置窗体标题</param>
public static void ShowMe(
Form owner,
MyThreadExecuting doSomething, object parameters,
MyThreadAborting aborting,
MyThreadFinished finished,
string windowCaption = "")
{
if (_Instance == null)
{
_Owner = owner;
_OnOperating = doSomething;
_Params = parameters;
_OnFinished = finished;
_OnAborting = aborting;
_Instance = new frmThreadOperating();
_Instance.Text = windowCaption.IsNullOrEmpty() ? "异步操作等待窗体" : windowCaption;
_Instance.ShowDialog(owner);
}
else
{
Msg.Warning("异步任务正在运行中,请完成此任务后再操作!");
}
}
//
//省略代码....
//
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网