c# windows 服务还是普通程序?
我正在尝试创建一个定期运行的程序(例如每 5 分钟),或者运行程序并让它执行其中的函数(每 5 分钟)。
该程序在执行时从数据库获取数据,然后将其写入(目前)例如 info.txt 文件(此处不包含敏感内容)。每次写入文件时,它都应该覆盖文件中的现有信息。
该程序也应该在 Windows 启动时自动启动。 (因此无需登录计算机并执行 .exe [如果它是普通程序而不是服务])
在执行程序的期间之间无需执行任何操作。
因此,我应该将此程序作为 Windows 服务运行,还是应该使用任务计划程序定期启动该程序来执行此操作? 我的目标是让这个程序尽可能顺利地运行而不堵塞资源。 (例如,它不需要超过 5% 的 cpu)
我希望我的问题足够清楚。
Possible Duplicates:
windows service vs scheduled task
Windows Service Vs Simple Program
I'm trying to create a program that runs periodically (say every 5 minutes), or have the program running and have it execute the function within it (every 5 minutes).
The program is to obtain data from a database when it's executed and then write this to (for now) say info.txt file (no sensitive stuff is contained in here). each time it writes to the file it should overwrite the existing info within the file.
The program should also be started automatically at windows start up. (thus no need to login on the machine and to execute the .exe [if it's a normal program and not a service])
In between the periods that it executes the program would have nothing to do.
Therefore, should I run this program as a Windows Service, or should I use the Task Scheduler to periodically start the program to do this?
My goal is for this program to run as smooth as possible without clogging up resources. (eg. it shouldn't need more than 5% of the cpu)
I hope my question was clear enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会选择由任务调度程序触发的应用程序。您唯一需要担心的是运行应用程序的单个实例。
您可以将任务设置为在特定用户帐户下运行,并且即使用户未登录也可以运行。有许多事件可以触发任务星,例如“Windows 启动”、“系统空闲”...
其他优点是:如果出现崩溃,您可以设置任务计划程序向您发送电子邮件或以多种方式提醒您。您可以控制应用程序的“退出代码”,并向任务调度程序发出信号,指示正在发生什么以及要做什么。
任务计划程序提供了很多积极的功能,但使用它们的人并不多。
I would go with application that is triggered by task scheduler. Only thing that you need to worry about is to run single instance of your application.
You can set task to run under specific user account and to run even if user is not logged on. There are number of events that can trigger task star like "Windows start", "System Idle"...
Other advantage is: If something crashes you can set task scheduler to send you an email or alert you in number of ways. You can control "exit code" of your application and signal to task scheduler what's going on and what to do.
There are a lot of positive features that task scheduler offers but not many people are using them.
我建议为此使用 Windows 服务。创建两者并比较资源使用情况可能是个好主意?
I would suggest a Windows Service for this. Might be a good idea to create both and compare what the resource usage is like anyway?
实际上,我建议根据您希望运行的任务的要求来选择两者。我通常将计划服务的大部分功能构建到单个类库中,然后将其包装在控制台应用程序中以启动和调试。当满意时,我将其包装在 Windows 服务中并忘记它。
使用控制台应用程序的注意事项:
使用窗口服务的注意事项
..还有很多事情需要考虑,但是正确编码,您可以从第一个开始,当您对此有信心时转向第二个。
I would actually recommend going for both depending on the requirements of the task you wish to run. I generally build most functionality for scheduled services into a single class library and then wrap it in a console application to start with and for debugging. When satisfied I wrap it in a windows service and forget about it.
Considerations of using a console app:
Considerations of using a window service
..there's a lot more to consider, but code it right and you can start with one and move to the 2nd when you're confident about it.