c# ado.net 多线程,每个线程定时(时间间隔)执行任务
管理“方案”
class TaskManager
{
TaskObject[] taskObject = new TaskObject[30];
IniOperate TaskConfi;
Form1 form;
string TaskName="";
public TaskManager()
{
}
public void setParameter(IniOperate TaskConfi, Form1 form, string TaskName)//传递参数
{
this.TaskConfi = TaskConfi;
this.form = form;
this.TaskName = TaskName;
}
public TaskObject getLeisureTaskObject()//获取空闲任务对象
{
int i=0;
for (i = 0; i < 30; i++)
{
if (taskObject[i] == null)
{
break;
}
}
taskObject[i] = new TaskObject(this.TaskConfi, this.form, this.TaskName);
return taskObject[i];
}
public void stopTask(string TaskName)//停止任务
{
for(int i=0;i<30;i++)
{
if(taskObject[i]!=null)
{
if(taskObject [i].TaskName .Equals(TaskName))
{
taskObject [i].stopTask();
taskObject [i]=null;
break ;
}
}
}
}
public void stopAllTask()//停止所有任务
{
for (int i = 0; i < 30; i++)
{
if (taskObject[i] != null)
{
taskObject[i].stopTask();
taskObject[i] = null;
}
}
}
}
一个方案里有多个任务(这些任务视为同类)
class TaskObject
{
SubTaskObject[] subTaskObject=new SubTaskObject [300];//子任务对象数组
SubTaskObject subTaskObjectTemp;
IniOperate Confi;//主配置文件
Form1 form;//主界面窗体引用
string[] subTaskName;//子任务配置文件名
IniOperate[] subTaskConfi=new IniOperate [300];//子任务配置文件对象数组
public string TaskName="";//任务名,用来标识大任务对象
public TaskObject(IniOperate Confi,Form1 form,string TaskName)
{
this.Confi = Confi;
this.form = form;
this.TaskName = TaskName;
this.getSubTaskName();
this.getSubTaskConfi();
}
public void getSubTaskName()//获取子配置文件名
{
string[] subTaskNameTemp= Confi.IniReadValue("任务描述", "SubTaskName").Split('|');
subTaskName = subTaskNameTemp;
}
public void getSubTaskConfi()//得到子任务配置文件对象数组
{
int i = 0;
foreach (string s in subTaskName)
{
if (!"".Equals(s) && s!=null && subTaskConfi[i]==null)//不为空
{
subTaskConfi[i] = new IniOperate(@".\Task\" + TaskName + @"\" + s);
}
i++;
}
}
public SubTaskObject getLeisureSubTaskObject(IniOperate subTaskName)//获取空闲的子任务对象
{
int i = 0;
for (i = 0; i < 300; i++)
{
if (subTaskObject[i] == null)
{
subTaskObject[i] = new SubTaskObject(subTaskName ,this .form,this .Confi,TaskName);
break;
}
}
return subTaskObject[i];//返回一个构造好的子任务对象
}
public void stopTask()//停止任务
{
for (int i = 0; i < 300; i++)
{
if (subTaskObject[i] != null)
{
subTaskObject[i].stopSubTask();
}
}
}
public void startTask()//逐个执行子任务
{
try
{
this.getSubTaskName();
this.getSubTaskConfi();
foreach (IniOperate confi in subTaskConfi)
{
if (confi != null)
{
subTaskObjectTemp = this.getLeisureSubTaskObject(confi);
subTaskObjectTemp.startSubTask();
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
任务类:
class SubTaskObject
{
System.Timers.Timer STimer = new System.Timers.Timer();
System.Timers.Timer TTimer = new System.Timers.Timer();
Form1 form;
IniOperate Confi;//子配置文件对象
IniOperate TaskConfi;//主配置文件对象
string TaskName = "";
bool isRunA = true;
bool isRunB = true;
string SyncType = "";
public SubTaskObject(IniOperate Confi,Form1 form,IniOperate TaskConfi,string TaskName)
{
this .Confi =Confi ;
this.form =form ;
this.TaskConfi = TaskConfi;
this.TaskName = TaskName;
STimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
TTimer.Elapsed +=new ElapsedEventHandler(OnTimedEventReverse);
STimer.Interval = Convert.ToInt32(TaskConfi.IniReadValue("任务描述", "TimerSpace")) * 1000;
SyncType = TaskConfi.IniReadValue("任务描述", "SyncType");
TTimer.Interval = STimer.Interval;
}
public void stopSubTask()//停止任务
{
if (SyncType.Equals("1"))
{
if ("单向".Equals(Confi.IniReadValue("taskType", "tasktype")))
{
STimer.Stop();
//STimer.Dispose();
}
else
{
STimer.Stop();
//STimer.Dispose();
TTimer.Stop();
//TTimer.Dispose();
}
}
if (SyncType.Equals("2"))
{
STimer.Stop();
//STimer.Dispose();
TTimer.Stop();
//TTimer.Dispose();
}
}
public void startSubTask()
{
try
{
if (SyncType.Equals("1"))
{
if (InterfaceHelper.validateDatabaseEnviroment(TaskConfi, Confi, TaskName))
{
if ("单向".Equals(Confi.IniReadValue("taskType", "tasktype")))
{
STimer.Start();
}
else
{
STimer.Start();
TTimer.Start();
}
}
}
if (SyncType.Equals("2"))
{
STimer.Start();
TTimer.Start();
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
if (isRunA)
{
isRunA = false;
ServerInterface.chooseSyncMethod(this.Confi, this.form, this.TaskConfi, 0);
isRunA = true;
}
}
private void OnTimedEventReverse(object source, ElapsedEventArgs e)
{
if (isRunB)
{
isRunB = false;
ServerInterface.chooseSyncMethod(this.Confi, this.form, this.TaskConfi, 1);
isRunB = true;
}
}
特点:不进行线程池的管理,利用多线程定时器自动调用,不必去管理线程,只需管理时钟的开启与关闭
实现功能:多个任务,定时时间间隔执行这些任务,也可以利用这个,满足多线程的一些需求。