如何在 WinForms 应用程序中安排任务?
问题:如何在 WinForms 应用程序中安排任务?即 (a) 最好的方法/.NET 类/方法是什么 (b) 如果有一个开源组件可以很好地做到这一点,那么会推荐哪一个。
背景:
- Winforms应用程序(.NET v3.5,C#,VS2008)
- 我假设我将始终运行winforms应用程序,并且在不使用时最小化到系统托盘
- 想要一个简单的方法(不想进入运行 UI winforms 应用程序与之通信的单独服务等)
- 希望能够让用户选择安排同步的频率(例如每小时、每天 - 选择时间等)
- 能够在调度程序触发时运行一个块代码(例如,假设它可以包装为后台工作任务)
- 应用程序始终运行并且可以运行。出现在系统托盘中
QUESTION: How can I schedule tasks in a WinForms app? That is either (a) what is the best approach / .NET classes/methods to use of (b) if there is an open source component that does this well which one would be recommended.
BACKGROUND:
- Winforms app (.NET v3.5, C#, VS2008)
- I'm assuming I will run the winforms application always, and just minimise to the system tray when not in use
- Want a simple approach (didn't want to get into separate service running that UI winforms app talks to etc)
- Want to be able to let the user select how often to schedule the sync (e.g. hourly, daily - pick time, etc)
- Ability to at the times when the scheduler fires to run a chunk of code (assume it could be wrapped as a backgroundworker task for example)
- The application is always running & appears in the system tray
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,Windows 中直接内置了一些东西,可以做到这一点。它称为Windows 任务计划程序!与其让 Windows 应用程序等待合适的时间来运行一段代码,不如直接使用底层系统实用程序并将该段代码存储在单独的可执行文件中运行:它是 更容易和更高效。
我之前使用过任务计划程序来配置我的应用程序以按照非常具体的计划启动。在 .NET 应用程序中执行此操作的最佳方法是使用
基本上,要完成您在问题中所述的内容,您需要制作一个提供 GUI 的 Windows 应用程序。该 GUI 应该具有调节任务的创建和更改的选项。该任务应该启动您必须运行的代码(您应该将其存储在单独的可执行文件中,可能作为透明的 WinForms 应用程序,因此是隐藏的。)
以下是一些代码 来自库本身的 CodeProject 文章,说明如何创建任务:
注意:
priority
选项对我来说从来不起作用,总是使应用程序崩溃。我建议您将其保留;通常,它不会产生那么大的差异。还有更多代码示例文章页面,其中一些展示了如何更改任务的设置、列出所有计划任务等。
There's actually something directly built into Windows that will do just that. It's called the Windows Task Scheduler! Rather than having a Windows application that sits and waits for the right time to run a piece of code, you'd be better off just using an underlying system utility and storing the piece of code to run in a separate executable: it's easier and more efficient.
I've used the Task Scheduler before to configure my applications to start on a pretty specific schedule. The best way to do it out of a .NET application is to use this handy little library.
Basically, to accomplish what you've stated in your question, you need to make a Windows application that provides a GUI. This GUI should have options that regulate the creation and alteration of a Task. The Task should launch the code you have to run (you should store it in a separate executable, probably as a WinForms app that's transparent and, thus, hidden.)
Here's some code from the CodeProject article of the library itself that illustrates how to create a task:
NOTE: The
priority
option never worked for me, always crashing the app. I recommend you leave it out; usually, it doesn't make that big a difference.There are more code samples on the article page, some of which show how to change the settings of a Task, list all Scheduled Tasks, etc.
如果您不需要 Windows 服务,则可以选择使用 Windows 启动应用程序。您可以使用以下代码来执行此操作。
安排同步是什么意思?它是差异服务吗?然后您可以使用计时器,然后将用户设置存储在 xml 文件或 DB 中。如果您想要简单的存储,则可以使用 My.设置。
编辑:我认为唯一的方法是检查应用程序启动的日期,然后定期检查日期。另一种选择是以编程方式使用任务计划程序。 Task Scheduler Managed Wrapper 是一个开源包装器,您可以尝试一下。
If you don't want a Windows service then starting the application with windows is an option. You can use the following code to do that.
What did you mean by schedule the sync? Is it a diff service? Then you can use the timer and then store the user settings in an xml file or a DB. If you want a simple storage then you can use My.Settings.
Edit: I think the only way is to check for the date when the app starts and then check the date periodically. Another option is to use the task scheduler programatically. Task Scheduler Managed Wrapper is an open source wrapper which you can try out.
那么您想运行应用程序中预定义代码而不是外部可执行文件的任务吗?
制作一个带有系统托盘图标的 winforms 应用程序。如果您使用单击一次进行部署,请将发布者名称设置为“Startup”,以便在 Startup 目录中安装快捷方式。
使用计时器检查计划并根据需要启动线程。将本地计划存储在 XML 文件中,以便可以使用 LINQ 轻松查询。
您正在使用后台工作程序运行任务,因此线程已在使用中。
但是,如果您想使用 Windows 任务计划程序,可以使用它的 API。不是一个优雅的模块,但它工作得足够好。
So you want to run tasks that are predefined code in your app and not external exes?
Make a winforms app with a system tray icon. If you are deploying using click once, make the Publisher name "Startup" so a shortcut installs in the Startup directory.
Use a timer to check against the schedule and launch threads if needed. Store you local schedule in an XML file so it can be easily queried with LINQ.
You are running your tasks with Background worker so threading is already in use.
If you wanted to use the Windows Task Scheduler however, there is an API for it. Not a graceful module but it works well enough.