C#+TaskScheduler(定时任务)实现定时自动下载

时间:2024-02-29 16:00:58
C# /TaskScheduler /定时任务 /定时自动下载 3410 实现原理,客户是广电,在广电服务器创建一个FTP目录,然后每天自动从卫星上自动更新节目列表, 然后功能就是要每天定点一个时间自动跟FTP目录中前一天下载下来的节目信息进行时间和大小对比,如果有差异则自动下载覆盖,让插入到数据库中,因此就想到了两个办法: 1:用服务来实现 http://blog.csdn.net/kongwei521/article/details/51626589 http://blog.csdn.net/kongwei521/article/details/51793036 2:windows定时任务 经过一段时间观察用服务来实现的 下载时间会越来越靠后,感觉不靠谱就想用定时任务来替换。 最终使用 Microsoft.Win32.TaskScheduler 来实现了可以使用,另外两个 FluentScheduler Quartz.Net 来实现, 效果图: 代码: 新建一个控制台应用程序: 然后写代码: using Microsoft.Win32.TaskScheduler; 第一种方法: [csharp] view plain copy // Get the service on the local machine TaskService ts = new TaskService(); // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "wei kong"; // Add a trigger that will fire the task at this time every other day DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 1 }); //设置任务起始时间 dt.StartBoundary = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 09:40:00")); //设置重复任务间隔时间 dt.Repetition.Interval = TimeSpan.FromDays(1); // Create an action that will launch Notepad whenever the trigger fires td.Actions.Add(new ExecAction(@"C:\AutoDownTvInfo.exe", "", null));//这里可参考http://blog.csdn.net/kongwei521/article/details/51793036下载 // Register the task in the root folder ts.RootFolder.RegisterTaskDefinition(@"Test", td); Thread.Sleep(2000); Console.WriteLine("计划任务结束" + DateTime.Now); Thread.Sleep(2000); 第二种方法: 使用方法: var compath = AppDomain.CurrentDomain.BaseDirectory +"AutoDownTvInfo.exe";//这里可参考http://blog.csdn.net/kongwei521/article/details/51793036下载 CreateTaskScheduler("kong wei", "AutoDownTvInfo", compath, "500"); [csharp] view plain copy /// delete task /// /// private static void DeleteTask(string taskName) { TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); folder.DeleteTask(taskName, 0); } /// /// get all tasks /// public static IRegisteredTaskCollection GetAllTasks() { TaskSchedulerClass ts = new TaskSchedulerClass(); ts.Connect(null, null, null, null); ITaskFolder folder = ts.GetFolder("\\"); IRegisteredTaskCollection tasks_exists = folder.GetTasks(1); return tasks_exists; } /// /// check task isexists /// /// /// public static bool IsExists(string taskName) { var isExists = false; IRegisteredTaskCollection tasks_exists = GetAllTasks(); for (int i = 1; i <= tasks_exists.Count; i++) { IRegisteredTask t = tasks_exists[i]; if (t.Name.Equals(taskName)) { isExists = true; break; } } return isExists; } /// /// create task /// /// /// /// /// /// state public static _TASK_STATE CreateTaskScheduler(string creator, string taskName, string path, string interval) { try { if (IsExists(taskName)) { DeleteTask(taskName); } //new scheduler TaskSchedulerClass scheduler = new TaskSchedulerClass(); //pc-name/ip,username,domain,password scheduler.Connect(null, null, null, null); //get scheduler folder ITaskFolder folder = scheduler.GetFolder("\\"); //set base attr ITaskDefinition task = scheduler.NewTask(0); task.RegistrationInfo.Author = "kong wei";//creator task.RegistrationInfo.Description = "每天自动定时下载节目信息";//description //set trigger (IDailyTrigger ITimeTrigger) 设置小时/分/秒来实现 // ITimeTrigger tt = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); // tt.Repetition.Interval = interval;// format PT1H1M==1小时1分钟 设置的值最终都会转成分钟加入到触发器 //设置每天特定的时间来实现 IDailyTrigger tt = (IDailyTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY); tt.StartBoundary = "2016-11-15T10:15:00"; //start time //set action IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); action.Path = path; task.Settings.ExecutionTimeLimit = "PT0S"; //运行任务时间超时停止任务吗? PTOS 不开启超时 task.Settings.DisallowStartIfOnBatteries = false;//只有在交流电源下才执行 task.Settings.RunOnlyIfIdle = false;//仅当计算机空闲下才执行 IRegisteredTask regTask = folder.RegisterTaskDefinition(taskName, task, (int)_TASK_CREATION.TASK_CREATE, null, //user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask.Run(null); return runTask.State; } catch (Exception ex) { throw ex; } } 参考资料: http://twinklers3.rssing.com/chan-3472162/all_p18.html http://tzedeck.seesaa.net/article/388838290.html http://ymlib.com/YMWorld/CS/P1/W3/P134/YMWCS134.html 原文链接:http://blog.csdn.net/kongwei521/article/details/53185788