实现任务调度(类windows的任务计划) 核心想使用 System.Threading.timer的TimerCallback来实现任务的调度但是针对于那种 指定 每周中的周几,如周1,2,5 的时间段为10:00-22:00 这种具体的抽象不知道该如何去实现。求高手给个思路。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void TimerProc(object state){}System.Threading.Timer t= new System.Threading.Timer(new TimerCallback(TimerProc), null, 0, 24*60*60 * 1000);//每24小时运行一次t.Change(1000, 1000);延迟 interface IScheduledTask{ bool IsDue(DateTime now); void Run(object state); bool CanRemove();}public class DailyTask : IScheduledTask{ class TimeSlot { public DayOfWeek day; public TimeSpan time; } DateTime today; WaitCallback task; List<TimeSlot> timeSlots = new List<TimeSlot>(); List<TimeSlot> todayTasks = new List<TimeSlot>(); public DailyTask(WaitCallback task) { this.task = task; } public void AddTimeSlot(DayOfWeek day, DateTime time) { timeSlots.Add(new TimeSlot() { day = day, time = time.TimeOfDay }); } #region IScheduledTask Members void IScheduledTask.Run(object state) { if (task != null) task(state); } bool IScheduledTask.IsDue(DateTime now) { if( today != now.Date ) { todayTasks.Clear(); todayTasks.AddRange(timeSlots); today = now.Date; } for(int i = 0; i<todayTasks.Count; i++) { if (todayTasks[i].day == now.DayOfWeek && todayTasks[i].time < now.TimeOfDay ) { todayTasks.RemoveAt(i); return true; } } return false; } bool IScheduledTask.CanRemove() { return false; } #endregion}public class SingleTask : IScheduledTask{ //...}class Program{ static void Main(string[] args) { IList<IScheduledTask> tasks = new List<IScheduledTask>(); { // read tasks from file or registry etc... DailyTask taskA = new DailyTask( delegate{Console.Write("task A");}); taskA.AddTimeSlot(DayOfWeek.Friday, new DateTime(1, 1, 1, 16, 0, 0)); taskA.AddTimeSlot(DayOfWeek.Monday, new DateTime(1, 1, 1, 16, 0, 0)); tasks.Add(taskA); } while (true) { System.Threading.Thread.Sleep(5000); DateTime now = DateTime.Now; for (int i = 0; i < tasks.Count; i++) { if (tasks[i].IsDue(now)) { ThreadPool.QueueUserWorkItem(tasks[i].Run); } if (tasks[i].CanRemove()) { tasks.RemoveAt(i--); } } } }} 用 Timer 定时(可以是每分钟)看一下系统时间,到了 10:00 就开始干活星期几也好办,也是看系统时间,到了星期一就干活其余时间统统睡觉去。 c# windorm 求助字符串比较问题 DataGridView的更新问题 help?? 使用VS2005的“附加到进程”方式调试的时候,VS2005死掉,系统要求发送错误报告……实在解决不了啦 C#怎样嵌套列出内容??参与有分 如何让一个层浮动于外层的最下方 我程序中datagrid的数据如何写到水晶报表(不通过数据库) 请问有什么方法接收到从声卡的麦克风口的波形数据?谢谢 请问怎样传输文件比较快 如何获取出网站下的所有页面地址 ASP.NET中如何对数组元素进行排序
{}
System.Threading.Timer t= new System.Threading.Timer(new TimerCallback(TimerProc), null, 0, 24*60*60 * 1000);//每24小时运行一次
t.Change(1000, 1000);延迟
{
bool IsDue(DateTime now);
void Run(object state);
bool CanRemove();
}
public class DailyTask : IScheduledTask
{
class TimeSlot
{
public DayOfWeek day; public TimeSpan time;
}
DateTime today;
WaitCallback task;
List<TimeSlot> timeSlots = new List<TimeSlot>();
List<TimeSlot> todayTasks = new List<TimeSlot>(); public DailyTask(WaitCallback task)
{
this.task = task;
}
public void AddTimeSlot(DayOfWeek day, DateTime time)
{
timeSlots.Add(new TimeSlot() { day = day, time = time.TimeOfDay });
} #region IScheduledTask Members
void IScheduledTask.Run(object state)
{
if (task != null) task(state);
}
bool IScheduledTask.IsDue(DateTime now)
{
if( today != now.Date )
{
todayTasks.Clear();
todayTasks.AddRange(timeSlots);
today = now.Date;
} for(int i = 0; i<todayTasks.Count; i++)
{
if (todayTasks[i].day == now.DayOfWeek && todayTasks[i].time < now.TimeOfDay )
{
todayTasks.RemoveAt(i);
return true;
}
}
return false;
}
bool IScheduledTask.CanRemove()
{
return false;
}
#endregion
}
public class SingleTask : IScheduledTask
{
//...
}class Program
{
static void Main(string[] args)
{
IList<IScheduledTask> tasks = new List<IScheduledTask>();
{
// read tasks from file or registry etc...
DailyTask taskA = new DailyTask( delegate{Console.Write("task A");});
taskA.AddTimeSlot(DayOfWeek.Friday, new DateTime(1, 1, 1, 16, 0, 0));
taskA.AddTimeSlot(DayOfWeek.Monday, new DateTime(1, 1, 1, 16, 0, 0));
tasks.Add(taskA);
} while (true)
{
System.Threading.Thread.Sleep(5000);
DateTime now = DateTime.Now;
for (int i = 0; i < tasks.Count; i++)
{
if (tasks[i].IsDue(now))
{
ThreadPool.QueueUserWorkItem(tasks[i].Run);
}
if (tasks[i].CanRemove())
{
tasks.RemoveAt(i--);
}
}
}
}
}