Gre*_*reg 6 .net scheduling .net-3.5 winforms
问题:如何在WinForms应用程序中安排任务?这是(a)使用(b)的最佳方法/ .NET类/方法是什么,如果有一个开源组件做得好,建议使用哪一个.
背景:
Max*_*sky 10
实际上,Windows中直接内置了一些功能.它被称为Windows任务计划程序!您可以更好地使用底层系统实用程序并将代码片段存储在单独的可执行文件中运行,而不是让Windows应用程序等待正确的时间来运行一段代码:它更容易,更多有效率.
我以前使用过任务计划程序来配置我的应用程序以非常具体的计划启动.从.NET应用程序中完成它的最佳方法是使用这个方便的小库.
基本上,要完成您在问题中所述的内容,您需要创建一个提供GUI的Windows应用程序.此GUI应具有调节任务的创建和更改的选项.任务应该启动你必须运行的代码(你应该将它存储在一个单独的可执行文件中,可能是一个透明的WinForms应用程序,因此被隐藏了.)
以下是库本身的CodeProject文章中的一些代码,用于说明如何创建任务:
//Get a ScheduledTasks object for the local computer.
ScheduledTasks st = new ScheduledTasks();
// Create a task
Task t;
try {
t = st.CreateTask("D checker");
} catch (ArgumentException) {
Console.WriteLine("Task name already exists");
return;
}
// Fill in the program info
t.ApplicationName = "chkdsk.exe";
t.Parameters = "d: /f";
t.Comment = "Checks and fixes errors on D: drive";
// Set the account under which the task should run.
t.SetAccountInformation(@"THEDOMAIN\TheUser", "HisPasswd");
// Declare that the system must have been idle for ten minutes before
// the task will start
t.IdleWaitMinutes = 10;
// Allow the task to run for no more than 2 hours, 30 minutes.
t.MaxRunTime = new TimeSpan(2, 30, 0);
// Set priority to only run when system is idle.
t.Priority = System.Diagnostics.ProcessPriorityClass.Idle;
// Create a trigger to start the task every Sunday at 6:30 AM.
t.Triggers.Add(new WeeklyTrigger(6, 30, DaysOfTheWeek.Sunday));
// Save the changes that have been made.
t.Save();
// Close the task to release its COM resources.
t.Close();
// Dispose the ScheduledTasks to release its COM resources.
st.Dispose();
Run Code Online (Sandbox Code Playgroud)
注意:该priority
选项从未对我有用,总是崩溃应用程序.我建议你把它留下来; 通常,它没有那么大的差别.
文章页面上有更多代码示例,其中一些示例显示如何更改任务的设置,列出所有计划任务等.
如果您不需要 Windows 服务,则可以选择使用 Windows 启动应用程序。您可以使用以下代码来执行此操作。
Dim regKey As RegistryKey
regKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue(Application.ProductName, Application.ExecutablePath)
regKey.Close()
Run Code Online (Sandbox Code Playgroud)
安排同步是什么意思?它是差异服务吗?然后您可以使用计时器,然后将用户设置存储在 xml 文件或 DB 中。如果您想要一个简单的存储,那么您可以使用My.Settings。
编辑:我认为唯一的方法是检查应用程序启动的日期,然后定期检查日期。另一种选择是以编程方式使用任务计划程序。Task Scheduler Managed Wrapper是一个开源包装器,您可以尝试一下。
归档时间: |
|
查看次数: |
14627 次 |
最近记录: |