namespace Ecode.Utility
{
public interface ISchedulerJob
{
void Execute();
int SleepInterval
{
get;
}
}
public class SchedulerConfiguration
{
//时间间隔
private int sleepInterval;
//任务列表
private ArrayList jobs = new ArrayList();
public int SleepInterval { get { return sleepInterval; } }
public ArrayList Jobs { get { return jobs; } }
//调度配置类的构造函数
public SchedulerConfiguration(int newSleepInterval)
{
sleepInterval = newSleepInterval;
}
}
public class Scheduler
{
private SchedulerConfiguration configuration = null;
public Scheduler(SchedulerConfiguration config)
{
configuration = config;
}
public void Start()
{
while (true)
{
//执行每一个任务
foreach (ISchedulerJob job in configuration.Jobs)
{
ThreadStart myThreadDelegate = new ThreadStart(job.Execute);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
Thread.Sleep(job.SleepInterval);
}
}
}
}
}
创建任务(测试代码):
public class DumpInBillBarCodeFromLES : ISchedulerJob
{
public int SleepInterval
{
get { return 1000 * 3; }
}
public void Execute()
{
// 这里写导数方法
//文件保存的物理路径,CSTest为虚拟目录名称,F:InetpubwwwrootCSTest为物理路径
string p = @"F:\";
//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改,
//SchedulerJob.txt就是我们所写的文件
string FILE_NAME = p + "\\SchedulerJob1.txt";
//取得当前服务器时间,并转换成字符串
string c = System.DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss");
//标记是否是新建文件的标量
bool flag = false;
//如果文件不存在,就新建该文件
if (!File.Exists(FILE_NAME))
{
flag = true;
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
//向文件写入内容
StreamWriter x = new StreamWriter(FILE_NAME, true, System.Text.Encoding.Default);
if (flag) x.Write("计划任务测试开始:");
x.Write(" " + c);
x.Close();
}
}
我在主线程调用
SchedulerConfiguration config = new SchedulerConfiguration(1000 * 30*60);
config.Jobs.Add(new DumpInBillDetailFromLES());
config.Jobs.Add(new DumpOutBillDetailFromLES());
config.Jobs.Add(new DumpInBillBarCodeFromLES());
config.Jobs.Add(new DumpOutBillBarCodeFromLES()); //按照相同的方式创建的测试代码
Scheduler scheduler = new Scheduler(config);
ThreadStart myThreadStart = new ThreadStart(scheduler.Start);
schedulerThread = new Thread(myThreadStart);
schedulerThread.Start();
每个任务设置的是 每隔3秒执行一次,在测试过程中,一切正常感觉不到差异,但现实的业务是定时取数,每个任务的取数频率都不一样,有的设置为1小时,
有的设置为6小时,各不一样,并且Execute方法也不是简单的写个文本,而是复杂的取数逻辑,当频率设大时,任务根本不能同时执行,而是只能执行第一个,
只有第一个任务的 Thread.Sleep 到时间了,才能执行第二个,晕啊,这哪儿是多线程啊,请问以上的代码怎么改,才能实现真正的定时多线程?
请高手指点,非常感谢!
6 个解决方案
#1
用 Threading.Timer类试试
#2
System.Timers.Timer
private List<System.Timers.Timer> timmerList = new List<System.Timers.Timer>();
private void AttachFile()
{
System.Timers.Timer timmer = new System.Timers.Timer();
timmer.Interval = 5000;
timmer.Elapsed += new System.Timers.ElapsedEventHandler(timmer_Elapsed);
timmer.Enabled = true;
timmerList.Add(timmer);
}
void timmer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer timmer = sender as System.Timers.Timer;
if(timmer != null)
{
timmer.Enabled = false;
timmer.Dispose();
//Your upload code
}
}
private List<System.Timers.Timer> timmerList = new List<System.Timers.Timer>();
private void AttachFile()
{
System.Timers.Timer timmer = new System.Timers.Timer();
timmer.Interval = 5000;
timmer.Elapsed += new System.Timers.ElapsedEventHandler(timmer_Elapsed);
timmer.Enabled = true;
timmerList.Add(timmer);
}
void timmer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer timmer = sender as System.Timers.Timer;
if(timmer != null)
{
timmer.Enabled = false;
timmer.Dispose();
//Your upload code
}
}
#3
楼上正解
#4
江大鱼的!!
#5
Thread StartApplyPause;
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
#6
Thread StartApplyPause;
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
#1
用 Threading.Timer类试试
#2
System.Timers.Timer
private List<System.Timers.Timer> timmerList = new List<System.Timers.Timer>();
private void AttachFile()
{
System.Timers.Timer timmer = new System.Timers.Timer();
timmer.Interval = 5000;
timmer.Elapsed += new System.Timers.ElapsedEventHandler(timmer_Elapsed);
timmer.Enabled = true;
timmerList.Add(timmer);
}
void timmer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer timmer = sender as System.Timers.Timer;
if(timmer != null)
{
timmer.Enabled = false;
timmer.Dispose();
//Your upload code
}
}
private List<System.Timers.Timer> timmerList = new List<System.Timers.Timer>();
private void AttachFile()
{
System.Timers.Timer timmer = new System.Timers.Timer();
timmer.Interval = 5000;
timmer.Elapsed += new System.Timers.ElapsedEventHandler(timmer_Elapsed);
timmer.Enabled = true;
timmerList.Add(timmer);
}
void timmer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer timmer = sender as System.Timers.Timer;
if(timmer != null)
{
timmer.Enabled = false;
timmer.Dispose();
//Your upload code
}
}
#3
楼上正解
#4
江大鱼的!!
#5
Thread StartApplyPause;
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
#6
Thread StartApplyPause;
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
private void Pause_Tick(object sender, EventArgs e)//Timer 的事件
{
ThreadStart CreateApplyPause = new ThreadStart(ApplyPause);
StartApplyPause = new Thread(CreateApplyPause);
StartApplyPause.Name = "AP";
StartApplyPause.Start();
this.Pause.Interval = (Convert.ToInt32(rus.Space_Pause())) * 60000;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/