C#+TaskScheduler(定時任務)實現定時自動下載

來源:互聯網
上載者:User

標籤:author   machine   控制   service   execution   name   convert   自動   path   

C# /TaskScheduler /定時任務 /定時自動下載 3410實現原理,客戶是廣電,在廣電伺服器建立一個FTP目錄,然後每天自動從衛星上自動更新節目列表, 然後功能就是要每天定點一個時間自動跟FTP目錄中前一天下載下來的節目資訊進行時間和大小對比,如果有差異則自動下載覆蓋,讓插入到資料庫中,因此就想到了兩個辦法:1:用服務來實現 http://blog.csdn.net/kongwei521/article/details/51626589 http://blog.csdn.net/kongwei521/article/details/517930362: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 /// /// /// /// /// /// 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.htmlhttp://tzedeck.seesaa.net/article/388838290.htmlhttp://ymlib.com/YMWorld/CS/P1/W3/P134/YMWCS134.html原文連結:http://blog.csdn.net/kongwei521/article/details/53185788

C#+TaskScheduler(定時任務)實現定時自動下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.