C# Windows Schedule task此次收购task下一步执行时间
最近进行了一次需求和Windows Schedule task相关职能,通过schedule,计算下一次运行task时间。
它用于由第三方DLL实现,以下网站,以下载来自:
http://taskscheduler.codeplex.com/
1. 在Schedule Task里添加一个task。命名为“testJing”, trigger设置为每五天
添加一个Trigger,例如以下图:
2. 使用VS2012,创建一个Console Application,命名为ScheduleTaskDemo
3.加入引用。
从上述的站点下载(http://taskscheduler.codeplex.com/releases/view/120747),解压后,找到Microsoft.Win32.TaskScheduler.dll(我用的是v4.0目录下的,我建的application的.NET framework 4.5)
4. 打开program.cs文件。代码例如以下:
方法一:
using Microsoft.Win32.TaskScheduler;
using System;
public static void Main(string[] args)
{
using (TaskService ts = new TaskService())
{
Task task = ts.FindTask("testJing");
DateTime nextRunTime = task.NextRunTime;
Console.WriteLine("NextRunTime:" + nextRunTime);
}
}
NextRunTime 就可以获取下次跑task的时间。
这是直接用Task的属性NextRunTime,以下是我自己写的方法获取next run time.
方法二:使用Task里的一系列属性来计算,代码例如以下:
public static void Main(string[] args)
{
try
{
DateTime? beginDateGMT = null;
DateTime? endDateGMT = null;
GetDaysInterval(out beginDateGMT, out endDateGMT);
if (!beginDateGMT.HasValue || !endDateGMT.HasValue)
{
throw new Exception("BeginDateGMT(" + beginDateGMT + ") or EndDateGMT(" + endDateGMT + ") is null. Please check schedule setting.");
}
else
{
Console.WriteLine("BeginDateGMT:" + beginDateGMT + ", EndDateGMT:" + endDateGMT);
}
}
catch (Exception ex)
{
throw;
} Console.Read();
}
private static void GetDaysInterval(out DateTime? beginDateGMT, out DateTime? endDateGMT)
{
using (TaskService ts = new TaskService())
{
Task task = ts.FindTask("testJing");
TaskTriggerType taskTriggerType = task.Definition.Triggers[0].TriggerType;
//// get current day, e.g. 2014/11/10 00:00:00
DateTime nowGMT = DateTime.Now;
nowGMT = new DateTime(nowGMT.Year, nowGMT.Month, nowGMT.Day, 0, 0, 0);
beginDateGMT = nowGMT;
endDateGMT = null;
switch (taskTriggerType)
{
case TaskTriggerType.Daily:
DailyTrigger dailyTrigger = task.Definition.Triggers[0] as DailyTrigger;
if (dailyTrigger.DaysInterval > 0)
{
endDateGMT = beginDateGMT.Value.AddDays(dailyTrigger.DaysInterval);
}
else
{
throw new Exception("ScheduleType is daily, but daysInterval <= 0.");
}
break;
case TaskTriggerType.Weekly:
WeeklyTrigger weeklyTrigger = task.Definition.Triggers[0] as WeeklyTrigger;
if (weeklyTrigger.WeeksInterval > 0)
{
endDateGMT = beginDateGMT.Value.AddDays(weeklyTrigger.WeeksInterval * 7);
}
else
{
throw new Exception("ScheduleType is weekly, but weeksInterval <= 0.");
} break;
case TaskTriggerType.MonthlyDOW:
case TaskTriggerType.Monthly:
switch (taskTriggerType)
{
case TaskTriggerType.Monthly:
MonthlyTrigger monthlyTrigger = task.Definition.Triggers[0] as MonthlyTrigger;
endDateGMT = beginDateGMT.Value.AddMonths(1);
break;
case TaskTriggerType.MonthlyDOW:
MonthlyDOWTrigger monthlyDOWTrigger = task.Definition.Triggers[0] as MonthlyDOWTrigger;
if ((monthlyDOWTrigger.WeeksOfMonth == WhichWeek.FirstWeek
|| monthlyDOWTrigger.WeeksOfMonth == WhichWeek.SecondWeek || monthlyDOWTrigger.WeeksOfMonth == WhichWeek.ThirdWeek
|| monthlyDOWTrigger.WeeksOfMonth == WhichWeek.FourthWeek || monthlyDOWTrigger.WeeksOfMonth == WhichWeek.LastWeek)
&& (monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Sunday
|| monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Monday || monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Tuesday
|| monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Wednesday || monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Thursday
|| monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Friday || monthlyDOWTrigger.DaysOfWeek == DaysOfTheWeek.Saturday))
{
DateTime nextMonthFirstDay = new DateTime(beginDateGMT.Value.AddMonths(1).Year, beginDateGMT.Value.AddMonths(1).Month, 1, 0, 0, 0);
DateTime nextMonthLastDay = nextMonthFirstDay.AddMonths(1).AddDays(-1);
if (monthlyDOWTrigger.WeeksOfMonth == WhichWeek.LastWeek)
{
DayOfWeek? dayOfWeek = GetDayOfWeek(monthlyDOWTrigger.DaysOfWeek);
while (nextMonthLastDay >= nextMonthFirstDay)
{
if (nextMonthLastDay.DayOfWeek == dayOfWeek.Value)
{
endDateGMT = nextMonthLastDay;
break;
}
nextMonthLastDay = nextMonthLastDay.AddDays(-1);
}
}
else
{
//// weeksOfMonth: 1,2,3,4; daysOfWeekForMonth:7,1,2,3,4,5,6;
int weeksOfMonthInt = 0;
switch (monthlyDOWTrigger.WeeksOfMonth)
{
case WhichWeek.FirstWeek:
weeksOfMonthInt = 1;
break;
case WhichWeek.SecondWeek:
weeksOfMonthInt = 2;
break;
case WhichWeek.ThirdWeek:
weeksOfMonthInt = 3;
break;
case WhichWeek.FourthWeek:
weeksOfMonthInt = 4;
break;
default:
weeksOfMonthInt = 0;
break;
} if (weeksOfMonthInt > 0 && weeksOfMonthInt <= 4)
{
DayOfWeek? dayOfWeek = GetDayOfWeek(monthlyDOWTrigger.DaysOfWeek);
int countDayOfWeek = 0;
while (nextMonthLastDay >= nextMonthFirstDay)
{
if (nextMonthFirstDay.DayOfWeek == dayOfWeek)
{
countDayOfWeek++;
if (countDayOfWeek == weeksOfMonthInt)
{
endDateGMT = nextMonthFirstDay;
break;
}
} nextMonthFirstDay = nextMonthFirstDay.AddDays(1);
}
}
else
{
throw new Exception("ScheduleType is monthly, but weeksOfMonth is not between 1 and 4.");
}
}
}
else
{
throw new Exception("ScheduleType is monthly, but weeksOfMonth or daysOfWeekForMonth has multiple values.");
}
break;
default:
throw new Exception("ScheduleType is monthly, but scheduleByMonthType is " + taskTriggerType + ".");
}
break;
default:
throw new Exception("ScheduleType(" + taskTriggerType + ") is not in (Daily;Weekly;Monthly;).");
}
}
}
private static DayOfWeek? GetDayOfWeek(DaysOfTheWeek daysOfTheWeek)
{
DayOfWeek? dayOfWeek = null;
switch (daysOfTheWeek)
{
case DaysOfTheWeek.Sunday:
dayOfWeek = DayOfWeek.Sunday;
break;
case DaysOfTheWeek.Monday:
dayOfWeek = DayOfWeek.Monday;
break;
case DaysOfTheWeek.Tuesday:
dayOfWeek = DayOfWeek.Tuesday;
break;
case DaysOfTheWeek.Wednesday:
dayOfWeek = DayOfWeek.Wednesday;
break;
case DaysOfTheWeek.Thursday:
dayOfWeek = DayOfWeek.Thursday;
break;
case DaysOfTheWeek.Friday:
dayOfWeek = DayOfWeek.Friday;
break;
case DaysOfTheWeek.Saturday:
dayOfWeek = DayOfWeek.Saturday;
break;
default:
throw new Exception("ScheduleType is monthly, but daysOfWeek is not between Sunday and Saturday.");
} return dayOfWeek;
}
执行。就可以。
结果和方法一是一样的。
注意:方法二里。对于Monthly里。不支持选择months。自己可自行扩展。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
C# Windows Schedule task此次收购task下一步执行时间的更多相关文章
- 【点滴积累,厚积薄发】windows schedule task的最小时间间隔是多少?
在一些业务系统中,经常会出现一些需要定时运行的任务,业界已有很多优秀的选择,比如windows schedule task,Quartz等等.在以前在新蛋的时候用的是内部封装的JobConsole,非 ...
- C# Task 多任务 限制Task并发数量
LimitedTaskScheduler: using System; using System.Collections.Concurrent; using System.Collections.Ge ...
- 【C# Task】理解Task中的ConfigureAwait配置同步上下文
原文:https://devblogs.microsoft.com/dotnet/configureawait-faq/ 作者:Stephen 翻译:xiaoxiaotank 静下心来,你一定会有收获 ...
- Task.Run Vs Task.Factory.StartNew
在.Net 4中,Task.Factory.StartNew是启动一个新Task的首选方法.它有很多重载方法,使它在具体使用当中可以非常灵活,通过设置可选参数,可以传递任意状态,取消任务继续执行,甚至 ...
- Task.Run Vs Task.Factory.StartNew z
在.Net 4中,Task.Factory.StartNew是启动一个新Task的首选方法.它有很多重载方法,使它在具体使用当中可以非常灵活,通过设置可选参数,可以传递任意状态,取消任务继续执行,甚至 ...
- MapReduce作业的map task和reduce task调度参数
MapReduce作业可以细分为map task和reduce task,而MRAppMaster又将map task和reduce task分为四种状态: 1.pending:刚启动但尚未向reso ...
- C# Task的使用---Task的启动
.NET 4.0包含的新名称空间System.Threading.Tasks,它包含的类抽象出了线程功能.任务表示应完成的某个单元的工作.这个单元的工作可以在单独的线程中运行,也可以以同步的方式启动一 ...
- .Net4.0如何实现.NET4.5中的Task.Run及Task.Delay方法
前言 .NET4.0下是没有Task.Run及Task.Delay方法的,而.NET4.5已经实现,对于还在使用.NET4.0的同学来说,如何在.NET4.0下实现这两个方法呢? 在.NET4.0下, ...
- 第八节:Task的各类Task<TResult>返回值以及通用线程的异常处理方案。
一. Task的各种返回值-Task<TResult> PS: 在前面章节,我们介绍了Task类开启线程.线程等待.线程延续的方式,但我们并没有关注这些方式的返回值,其实他们都是有返回值的 ...
随机推荐
- PHP读取Excel里的文件
下载phpExcelReader http://sourceforge.net/projects/phpexcelreader 解压后得到以下这些文件 jxlrwtest.xls这个excel文件有 ...
- 使用Intellij Idea生成可执行文件jar,开关exe文件步骤
确保其Java代码是没有问题的,在IDEA常执行的,然后.按以下步骤: 步骤一:打开File -> Project Structure -> Artifacts,例如以下图 步骤二:点击& ...
- c# 获取某个对象的[公有属性]的名称,类型,值
/// <summary> /// 获取某个对象的[公有属性]的名称,类型,值 /// </summary> /// <typeparam name="T&qu ...
- java基础程序题
发现自己初学java时保存在word里的练习题,哈哈,放博客里面来作为纪念吧~~~ [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔 ...
- OWIN编写中间件
OWIN系列之自己动手编写中间件 一.前言 1.基于OWIN的项目摆脱System.Web束缚脱颖而出,轻量级+跨平台,使得ASP.NET应用程序只需依赖这个抽象接口,不用关心所运行的Web服务器. ...
- OpenCV+MFC显示图像
1.首先下载openCV. 2.安装OpenCV.现在的版本号2.4.9,你并不需要配置环境变量. 3.设置包括文件夹,设定project库文件夹. 4.配置链接库.注意,链接库包含了文件名中包含一个 ...
- 获取Winform窗体、工作区 宽度、高度、命名空间、菜单栏高度等收集
MessageBox.Show("当前窗体标题栏高"+(this.Height - this.ClientRectangle.Height).ToString());//当前窗体标 ...
- HDU ACM 1267 下沙的沙子有几粒?->DP
题意:m个H和n个D,从左開始数H的累积个数总不比D的累计数少的排列有多少种.比如,3个H和1个D共同拥有3种符合要求的排列H D H H,H H D H,H H H D. 分析:状态方程为,DP[ ...
- UML部署图和图九组件图
前言 UML大部分描写叙述了逻辑和设计方面的信息.实现图用来描写叙述实现方面的信息.实现图包含部署图和构件图. 构件图 1. 概念 构件图从软件架构的角度来描写叙述一个系统的 ...
- Java多线程<1>
1.Java多线程的概念: 线(Thread):它指的是一个任务的从开始执行流程到结束. 穿线提供执行任务的机构.供java条款.在一个程序可以启动多个并发线程.候执行. 在单处理器系统中,多个线程共 ...