C#异步操作等待窗体,异步多线程处理数据通用界面(frmThreadOperating)
作者:作者不详  发布日期:2020-05-10 14:12:29
  C#异步操作等待窗体,异步多线程处理数据通用界面(frmThreadOperating)

C#异步操作等待窗体,异步多线程处理数据通用界面(frmThreadOperating)


frmThreadOperating界面简介

设计了一个异步多线程处理数据的通用界面(C#异步操作等待窗体)frmThreadOperating,用于查询大数据、批量处理大数据时提高用户体验。

比如要实现大批量数据跨业务系统推送,如下图所示:


贴图图片-CSharp异步操作等待窗体1





点击【查询】按钮开始分析数据并返回查询结果:

贴图图片-CSharp异步操作等待窗体2


点击【批量共享】或【反共享】按钮开始批量共享数据:

贴图图片-CSharp异步操作等待窗体3


【查询】按钮事件:

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




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



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



【查询】按钮事件调用ShowMe方法,对应的3个委托事件:


1.查询数据


C# Code:

private object ToQuery(object param)
{
  
//查询数据,返回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



3.查询时取消线程


C# Code:

private bool ToQueryAborting()
{
  
//允许查询的时候中断线程
  
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



贴图图片-CSharp异步操作等待窗体4



本实例源码仅CSFramework.V5旗舰版集成,若其他版本用户需要,请联系作者。



C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务




上一篇 下一篇