#region
|
|
using System;
|
using System.Threading;
|
using System.Windows.Forms;
|
using CSFrameworkV5.Common;
|
using CSFrameworkV5.Core;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library
|
{
|
/// <summary>
|
/// 异步线程处理通用界面(异步操作等待窗体)
|
/// </summary>
|
public partial class frmThreadOperating : frmBase
|
{
|
private static frmThreadOperating _Instance;
|
|
private static Thread _Thread;
|
private static Form _Owner;
|
private static object _Params; //MyThreadExecuting事件的参数
|
|
private static MyThreadExecuting _OnOperating;
|
private static MyThreadFinished _OnFinished;
|
private static MyThreadAborting _OnAborting;
|
|
//计时器读秒
|
private int _Secondes;
|
|
private frmThreadOperating()
|
{
|
InitializeComponent();
|
}
|
|
private void btnStop_Click(object sender, EventArgs e)
|
{
|
//若用户提供_OnAborting事件,在线程结束前,要通知该事件。
|
if (_OnAborting != null || _OnAborting())
|
{
|
if (_Thread != null) _Thread.Abort(); //结束线程
|
|
_Thread = null;
|
_Instance = null;
|
Close();
|
}
|
}
|
|
/// <summary>
|
/// 关闭窗体
|
/// </summary>
|
/// <param name="owner"></param>
|
public static void CloseMe(Form owner)
|
{
|
if (_Instance != null)
|
try
|
{
|
_Instance.Hide();
|
_Instance.Close();
|
_Instance = null;
|
owner.Show();
|
GC.Collect();
|
}
|
catch
|
{
|
}
|
}
|
|
private void DoCloseMe()
|
{
|
Close();
|
_Instance = null;
|
}
|
|
private void frmThreadOperating_Load(object sender, EventArgs e)
|
{
|
//开始计时
|
_Secondes = 1;
|
timer1.Start();
|
|
//启动线程
|
_Thread = new Thread(OnThreadStart);
|
_Thread.Start();
|
}
|
|
private void frmWaitingThread_FormClosing(object sender,
|
FormClosingEventArgs e)
|
{
|
timer1.Stop();
|
labelControl1.Text = "系统正在中断线程.....";
|
Application.DoEvents();
|
|
if (_Thread != null && _Thread.ThreadState == ThreadState.Running)
|
{
|
if (Msg.AskQuestion("确定要取消操作吗?"))
|
{
|
//若用户提供_OnAborting事件,在线程结束前,要通知该事件。
|
if (_OnAborting != null || _OnAborting())
|
{
|
e.Cancel = false;
|
_Thread.Abort();
|
_Thread = null;
|
}
|
}
|
else
|
{
|
e.Cancel = true;
|
}
|
}
|
|
if (!e.Cancel)
|
{
|
_Instance = null;
|
_Thread = null;
|
|
GC.Collect();
|
}
|
}
|
|
private void OnThreadStart()
|
{
|
object result = null;
|
|
try
|
{
|
//开始操作,比如查询大数据、批量处理大数据
|
result = _OnOperating(_Params);
|
|
//关闭当前窗体
|
if (_Instance != null)
|
_Instance.Invoke(new MyThreadClose(_Instance.DoCloseMe));
|
|
//操作完成,异步通知用户已完成数据处理
|
if (_Owner != null && _OnFinished != null)
|
_Owner.Invoke(new MyThreadFinished(_OnFinished), result);
|
|
_Thread = null;
|
}
|
catch (Exception ex) //线程异常事件处理
|
{
|
//if (ex.Message != "正在中止线程。") Msg.ShowException(ex);
|
|
//线程结束事件
|
if (_OnFinished != null)
|
_Owner.Invoke(new MyThreadFinished(_OnFinished), result);
|
|
//关闭窗体
|
if (_Instance != null)
|
_Instance.Invoke(new MyThreadClose(_Instance.DoCloseMe));
|
|
_Thread = null;
|
}
|
}
|
|
/// <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 = null,
|
MyThreadAborting aborting = null,
|
MyThreadFinished finished = null,
|
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("异步任务正在运行中,请完成此任务后再操作!");
|
}
|
}
|
|
private void timer1_Tick(object sender, EventArgs e)
|
{
|
labelControl2.Text = "用时 " + _Secondes.ToStringEx() + " 秒";
|
_Secondes++;
|
}
|
|
#region 自定义委托类型
|
|
public delegate object MyThreadExecuting(object param);
|
|
public delegate void MyThreadFinished(object data);
|
|
public delegate bool MyThreadAborting(); //线程终止事件,必须返回True;
|
|
public delegate void MyThreadClose();
|
|
#endregion
|
}
|
}
|