C# winform 任务计划程序疑问,高手来!
声明:此段程序是在其他人博客里拷贝过来的,现在有些疑问希望大家给点提示,第一次做,很生疏!
using System;
using System.Collections;
using System.Threading;
namespace POSTiming
{
#region 任务计划接口和一些标准实现
/// <summary>
/// 计划的接口
/// </summary>
public interface ISchedule
{
/// <summary>
/// 返回最初计划执行时间
/// </summary>
DateTime ExecutionTime
{
get;
set;
}
/// <summary>
/// 初始化执行时间于现在时间的时间刻度差
/// </summary>
long DueTime
{
get;
}
/// <summary>
/// 循环的周期
/// </summary>
long Period
{
get;
}
}
/// <summary>
/// 计划立即执行任务
/// </summary>
public class ImmediateExecution : ISchedule
{
#region ISchedule 成员
public DateTime ExecutionTime
{
get
{
// TODO: 添加 ImmediatelyShedule.ExecutionTime getter 实现
return DateTime.Now;
}
set
{
;
}
}
public long DueTime
{
get
{
return 0;
}
}
public long Period
{
get
{
// TODO: 添加 ImmediatelyShedule.Period getter 实现
return Timeout.Infinite;
}
}
#endregion
}
/// <summary>
/// 计划在某一未来的时间执行一个操作一次,如果这个时间比现在的时间小,就变成了立即执行的方式
/// </summary>
public class ScheduleExecutionOnce : ISchedule
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="schedule">计划开始执行的时间</param>
public ScheduleExecutionOnce(DateTime schedule)
{
m_schedule = schedule;
}
private DateTime m_schedule;
#region ISchedule 成员
public DateTime ExecutionTime
{
get
{
// TODO: 添加 ScheduleExecutionOnce.ExecutionTime getter 实现
return m_schedule;
}
set
{
m_schedule = value;
}
/// <summary>
/// 得到该计划还有多久才能运行
/// </summary>
public long DueTime
{
get
{
long ms = (m_schedule.Ticks - DateTime.Now.Ticks) / 10000;
if (ms < 0) ms = 0;
return ms;
}
}
public long Period
{
get
{
// TODO: 添加 ScheduleExecutionOnce.Period getter 实现
return Timeout.Infinite;
}
}
#endregion
}
/// <summary>
/// 周期性的执行计划
/// </summary>
public class CycExecution : ISchedule
{
/// <summary>
/// 构造函数,在一个将来时间开始运行
/// </summary>
/// <param name="shedule">计划执行的时间</param>
/// <param name="period">周期时间</param>
public CycExecution(DateTime shedule, TimeSpan period)
{
m_schedule = shedule;
m_period = period;
}
/// <summary>
/// 构造函数,马上开始运行
/// </summary>
/// <param name="period">周期时间</param>
public CycExecution(TimeSpan period)
{
m_schedule = DateTime.Now;
m_period = period;
}
private DateTime m_schedule;
private TimeSpan m_period;
#region ISchedule 成员
public long DueTime
{
get
{
long ms = (m_schedule.Ticks - DateTime.Now.Ticks) / 10000;
if (ms < 0) ms = 0;
return ms;
}
}
publc DateTime ExecutionTime
{
get
{
// TODO: 添加 CycExecution.ExecutionTime getter 实现
return m_schedule;
}
set
{
m_schedule = value;
}
}
public long Period
{
get
{
// TODO: 添加 CycExecution.Period getter 实现
return m_period.Ticks / 10000;
}
}
#endregion
}
#endregion