线程写法
2015-10-25 18:49:11 访问(2525) 赞(0) 踩(0)
// --- BaseThreadClass 线程的基础操作类 -- //
using System;
using System.Collections.Generic;
using System.Text;
namespace SlowX.WebManage.Classes.ThreadClasses
{
/// <summary>
/// 线程的基础操作
/// </summary>
public class BaseThreadClass
{
/// <summary>
/// 执行函数委托
/// </summary>
public ProcessRun runName;
/// <summary>
/// 数据参数
/// </summary>
public object data;
/// <summary>
/// 执行函数委托
/// </summary>
/// <param name="d"></param>
public delegate void ProcessRun(object d);
/// <summary>
/// 构造函数
/// </summary>
public BaseThreadClass()
{
}
/// <summary>
/// 执行函数
/// </summary>
public void RunProcess()
{
runName(data);
}
}
}
// -- 调用类 -- //
using System;
using System.Collections.Generic;
using System.Text;
namespace SlowX.WebManage.Classes.ThreadClasses
{
/// <summary>
///
/// </summary>
[Serializable]
public class MsgFormCloseConfig
{
/// <summary>
///
/// </summary>
public MsgFormCloseConfig()
{
}
/// <summary>
/// 当前提示的GUID
/// </summary>
protected int m_SleepTime = 5000;
/// <summary>
/// 当前提示的GUID
/// </summary>
public int SleepTime
{
get
{
return m_SleepTime;
}
set
{
m_SleepTime = value;
}
}
}
}
// 调用相关代码 //
/// <summary>
/// 设置状态栏提示文字
/// </summary>
/// <param name="SleepTime"></param>
public void SetAutoClosed(int SleepTime)
{
if (SleepTime <= 0)
return;
MsgFormCloseConfig config = new MsgFormCloseConfig();
config.SleepTime = SleepTime;
try
{
BaseThreadClass bt = new BaseThreadClass();
bt.data = config;
bt.runName = new BaseThreadClass.ProcessRun(DoCloseThread);
Thread execThread = new Thread(new ThreadStart(bt.RunProcess));
execThread.IsBackground = true;
execThread.Start();
}
catch // (Exception err)
{
// ClientBAHelper.instance.ExcuteException(this, err);
}
}
/// <summary>
/// 执行关闭的线程
/// </summary>
/// <param name="objClass"></param>
private void DoCloseThread(object objClass)
{
try
{
MsgFormCloseConfig config
=
objClass as MsgFormCloseConfig;
if (config == null)
{
return;
}
System.Threading.Thread.Sleep(config.SleepTime);
ThreadCloseForm();
}
catch // (Exception err)
{
}
finally
{
}
}
/// <summary>
///
/// </summary>
delegate void ThreadCloseFormCallback();
/// <summary>
///
/// </summary>
protected void ThreadCloseForm()
{
if (this.checkBox_IsClose.InvokeRequired)
{
ThreadCloseFormCallback d = new ThreadCloseFormCallback(ThreadCloseForm);
this.Invoke(d, new object[] { });
}
else
{
if (!checkBox_IsClose.Checked)
{
checkBox_IsClose.Checked = true;
this.Close();
}
}
}
标签:
线程写法 


上一条:
下一条:
相关评论
发表评论