📄 trigger.cs
字号:
/// Disposes Trigger
/// </summary>
public void Dispose()
{
iTaskTrigger = null;
}
#endregion
}
/// <summary>
/// Base class for all triggers that have a start time
/// </summary>
public abstract class StartableTrigger : Trigger
{
/// <summary>
/// Base constructor
/// </summary>
internal StartableTrigger() : base()
{
}
/// <summary>
/// Internal copy constructor
/// </summary>
/// <param name="iTrigger">Trigger to copy</param>
internal StartableTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
/// <summary>
/// Sets the start time of the task
/// </summary>
/// <param name="hour">Hour of the day that the trigger will start</param>
/// <param name="minute">Minutes of the hour</param>
/// <exception cref="ArgumentOutOfRangeException">The hour is not between 0 and 23 or the minute is not between 0 and 59.</exception>
internal void SetStartTime(ushort hour, ushort minute)
{
if (hour < 0 || hour > 23)
throw new ArgumentOutOfRangeException("hour", hour, "hour must be between 0 and 23");
if (minute < 0 || minute > 59)
throw new ArgumentOutOfRangeException("minute", minute, "minute must be between 0 and 59");
taskTrigger.StartHour = hour;
taskTrigger.StartMinute = minute;
}
/// <summary>
/// Gets hour of the day that trigger will start (24 hour clock)
/// </summary>
public short StartHour
{
get
{
return (short)taskTrigger.StartHour;
}
}
/// <summary>
/// Gets minutes of the hour (specified in <see cref="StartHour"/>) that trigger will start
/// </summary>
public short StartMinute
{
get
{
return (short)taskTrigger.StartMinute;
}
}
}
/// <summary>
/// Trigger that is only run once
/// </summary>
public class RunOnceTrigger : StartableTrigger
{
/// <summary>
/// Create a trigger to be run once at the specified time
/// </summary>
/// <param name="runDateTime">Date and time to execute trigger</param>
public RunOnceTrigger(DateTime runDateTime)
{
taskTrigger.BeginYear = (ushort)runDateTime.Year;
taskTrigger.BeginMonth = (ushort)runDateTime.Month;
taskTrigger.BeginDay = (ushort)runDateTime.Day;
SetStartTime((ushort)runDateTime.Hour, (ushort)runDateTime.Minute);
taskTrigger.Type = TaskTriggerType.TIME_TRIGGER_ONCE;
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal RunOnceTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
}
/// <summary>
/// Trigger that will run at specified time every day
/// </summary>
public class DailyTrigger : StartableTrigger
{
/// <summary>
/// Creates trigger that will run at specified time on a daily interval
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysInterval">Number of days between task runs</param>
public DailyTrigger(short hour, short minutes, short daysInterval)
{
SetStartTime((ushort)hour, (ushort)minutes);
taskTrigger.Type = TaskTriggerType.TIME_TRIGGER_DAILY;
taskTrigger.Data.daily.DaysInterval = (ushort)daysInterval;
}
/// <summary>
/// Creates trigger that will run at specified time every day
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
public DailyTrigger(short hour, short minutes) : this(hour, minutes, 1)
{
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal DailyTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
/// <summary>
/// Gets the number of days between task runs
/// </summary>
public short DaysInterval
{
get
{
return (short)taskTrigger.Data.daily.DaysInterval;
}
}
}
/// <summary>
/// Trigger that will run on a regular weekly interval
/// </summary>
public class WeeklyTrigger : StartableTrigger
{
/// <summary>
/// Creates a trigger that will run on specific days of the week at a specified time on a regular weekly interval
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfTheWeek">Days of the week task will run</param>
/// <param name="weeksInterval">Number of weeks between task runs</param>
public WeeklyTrigger(short hour, short minutes, DaysOfTheWeek daysOfTheWeek, short weeksInterval)
{
SetStartTime((ushort)hour, (ushort)minutes);
taskTrigger.Type = TaskTriggerType.TIME_TRIGGER_WEEKLY;
taskTrigger.Data.weekly.WeeksInterval = (ushort)weeksInterval;
taskTrigger.Data.weekly.DaysOfTheWeek = (ushort)daysOfTheWeek;
}
/// <summary>
/// Creates a trigger that will run on specific days of the week at a specified time every week
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfTheWeek">Days of the week task will run</param>
public WeeklyTrigger(short hour, short minutes, DaysOfTheWeek daysOfTheWeek) : this(hour, minutes, daysOfTheWeek, 1)
{
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal WeeklyTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
/// <summary>
/// Gets number of weeks between task runs
/// </summary>
public short WeeksInterval
{
get
{
return (short)taskTrigger.Data.weekly.WeeksInterval;
}
}
/// <summary>
/// Gets the days of the week on which the task will run
/// </summary>
public DaysOfTheWeek WeekDays
{
get
{
return (DaysOfTheWeek)taskTrigger.Data.weekly.DaysOfTheWeek;
}
}
}
/// <summary>
/// Trigger that will run on the specified days of the week for
/// the specified weeks in the specified months at the specified time.
/// </summary>
public class MonthlyDOWTrigger : StartableTrigger
{
/// <summary>
/// Creates a trigger that will run on the specified days of the week for
/// the specified weeks in the specified months at the specified time.
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfTheWeek">Days of the week task will run</param>
/// <param name="whichWeeks">Weeks of the month task will run</param>
/// <param name="months">Months of the year task will run</param>
public MonthlyDOWTrigger(short hour, short minutes, DaysOfTheWeek daysOfTheWeek, WhichWeek whichWeeks, MonthsOfTheYear months)
{
SetStartTime((ushort)hour, (ushort)minutes);
taskTrigger.Type = TaskTriggerType.TIME_TRIGGER_MONTHLYDOW;
taskTrigger.Data.monthlyDOW.WhichWeek = (ushort)whichWeeks;
taskTrigger.Data.monthlyDOW.DaysOfTheWeek = (ushort)daysOfTheWeek;
taskTrigger.Data.monthlyDOW.Months = (ushort)months;
}
/// <summary>
/// Creates a trigger that will run on the specified days of the week for
/// the specified weeks every month at the specified time.
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfTheWeek">Days of the week task will run</param>
/// <param name="whichWeeks">Weeks of the month task will run</param>
public MonthlyDOWTrigger(short hour, short minutes, DaysOfTheWeek daysOfTheWeek, WhichWeek whichWeeks) :
this(hour, minutes, daysOfTheWeek, whichWeeks,
MonthsOfTheYear.January|MonthsOfTheYear.February|MonthsOfTheYear.March|MonthsOfTheYear.April|MonthsOfTheYear.May|MonthsOfTheYear.June|MonthsOfTheYear.July|MonthsOfTheYear.August|MonthsOfTheYear.September|MonthsOfTheYear.October|MonthsOfTheYear.November|MonthsOfTheYear.December)
{
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal MonthlyDOWTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
/// <summary>
/// Gets weeks of the month task will run
/// </summary>
public short WhichWeeks
{
get
{
return (short)taskTrigger.Data.monthlyDOW.WhichWeek;
}
}
/// <summary>
/// Gets days of the week task will run
/// </summary>
public DaysOfTheWeek WeekDays
{
get
{
return (DaysOfTheWeek)taskTrigger.Data.monthlyDOW.DaysOfTheWeek;
}
}
/// <summary>
/// Gets months of the year task will run
/// </summary>
public MonthsOfTheYear Months
{
get
{
return (MonthsOfTheYear)taskTrigger.Data.monthlyDOW.Months;
}
}
}
/// <summary>
/// Trigger that will run on the specified days of the specified months
/// at the specified time
/// </summary>
public class MonthlyTrigger : StartableTrigger
{
/// <summary>
/// Creates a trigger that will run on the specified days of the specified months
/// at the specified time
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfMonth">Days of the month task will run</param>
/// <param name="months">Months of the year task will run</param>
public MonthlyTrigger(short hour, short minutes, int[] daysOfMonth, MonthsOfTheYear months)
{
SetStartTime((ushort)hour, (ushort)minutes);
taskTrigger.Type = TaskTriggerType.TIME_TRIGGER_MONTHLYDATE;
taskTrigger.Data.monthlyDate.Months = (ushort)months;
System.Collections.Specialized.BitVector32 bits = new System.Collections.Specialized.BitVector32(0);
foreach (uint i in daysOfMonth)
bits[(int)Math.Pow(2, i-1)] = true;
taskTrigger.Data.monthlyDate.Days = (uint)bits.Data;
}
/// <summary>
/// Creates a trigger that will run on the specified days every month
/// at the specified time
/// </summary>
/// <param name="hour">Hour of day task will start</param>
/// <param name="minutes">Minutes of hour (specified in "hour") task will start</param>
/// <param name="daysOfMonth">Days of the month task will run</param>
public MonthlyTrigger(short hour, short minutes, int[] daysOfMonth) :
this(hour, minutes, daysOfMonth,
MonthsOfTheYear.January|MonthsOfTheYear.February|MonthsOfTheYear.March|MonthsOfTheYear.April|MonthsOfTheYear.May|MonthsOfTheYear.June|MonthsOfTheYear.July|MonthsOfTheYear.August|MonthsOfTheYear.September|MonthsOfTheYear.October|MonthsOfTheYear.November|MonthsOfTheYear.December)
{
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal MonthlyTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
/// <summary>
/// Gets months of the year task will run
/// </summary>
public MonthsOfTheYear Months
{
get
{
return (MonthsOfTheYear)taskTrigger.Data.monthlyDate.Months;
}
}
/// <summary>
/// Gets days of the month task will run
/// </summary>
public int[] Days
{
get
{
uint[] ret = new uint[31];
uint cpos = 0;
System.Collections.Specialized.BitVector32 bits = new System.Collections.Specialized.BitVector32((int)taskTrigger.Data.monthlyDate.Days);
for (int i = 0; i < 32; i++)
if (bits[(int)Math.Pow(2, i-1)]) ret[cpos++] = (uint)i;
int[] nret = new int[cpos];
for (int i = 0; i < cpos; i++)
nret[i] = (int)ret[i];
return nret;
}
}
}
/// <summary>
/// Trigger that will run when the system is idle.
/// Dependent on values set in task. See <see cref="Task.IdleWaitMinutes"/>.
/// </summary>
public class OnIdleTrigger : Trigger
{
/// <summary>
/// Creates a trigger that will run when the system is idle.
/// Dependent on values set in task. See <see cref="Task.IdleWaitMinutes"/>.
/// </summary>
public OnIdleTrigger()
{
taskTrigger.Type = TaskTriggerType.EVENT_TRIGGER_ON_IDLE;
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal OnIdleTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
}
/// <summary>
/// Trigger that will run when the system starts.
/// </summary>
public class OnSystemStartTrigger : Trigger
{
/// <summary>
/// Creates a trigger that will run when the system starts.
/// </summary>
public OnSystemStartTrigger()
{
taskTrigger.Type = TaskTriggerType.EVENT_TRIGGER_AT_SYSTEMSTART;
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal OnSystemStartTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
}
/// <summary>
/// Trigger that will run when the user logs on.
/// </summary>
public class OnLogonTrigger : Trigger
{
/// <summary>
/// Creates a trigger that will run when the user logs on.
/// </summary>
public OnLogonTrigger()
{
taskTrigger.Type = TaskTriggerType.EVENT_TRIGGER_AT_LOGON;
}
/// <summary>
/// Internal constructor to create from existing Trigger
/// </summary>
/// <param name="iTrigger">Current base Trigger</param>
internal OnLogonTrigger(ITaskTrigger iTrigger) : base(iTrigger)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -