自动启动Windows服务几个小时,然后每天自动停止 [英] Automatically start windows service for some hours and then stop automatically everyday

查看:866
本文介绍了自动启动Windows服务几个小时,然后每天自动停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个窗口服务,应该在特定时间自动启动,并在每天运行几个小时后自动停止。我在c#.net处理,我需要指导才能达到我的需要。





在此先感谢:)



我尝试了什么:



我在Windows中尝试过.net c#编码service onstart和onstop事件

Hi All,

I have a window service that should automatically start at particular time and stop automatically after some hours run everyday. i am handling in c#.net and i need guidance to achieve exactly what i need.


Thanks in Advance :)

What I have tried:

I have tried .net c# coding in windows service onstart and onstop event

推荐答案

您可以使用 Windows任务计划程序执行此操作,请参阅:如何使用任务计划程序重新启动Windows服务 - 堆栈溢出 [ ^ ]

如果你想从代码中调用任务调度程序,我建议将其称为DOS命令,API库非常复杂。
You can use the Windows Task Scheduler to do this, see: How to restart a windows service using Task Scheduler - Stack Overflow[^]
If you want to call the Task Scheduler from code I would advise calling it as a DOS command, the API libraries are very complex.


OnStart和OnStop事件是服务启动和停止时发送到您的服务 - 处理他们不会让您的服务开始或让您的服务停止。他们在那里,所以你的服务可以做出适当的反应 - 加载和保存进度也许 - 而不是让它工作。



相反,在启动时运行你的服务,在里面定期检查时间。如果是时候开始处理,请这样做。如果是时候停止,请停止处理。
The OnStart and OnStop events are sent to your service when the service is started and stopped - handling them does not "make your service start" or "make your service stop" . They are there so your service can react appropriately - loading and saving progress perhaps - rather than to "get it working".

Instead, run your service at startup, and inside it check the time at regular intervals. If it's time to start processing, do so. If it's time to stop, cease processing.


这篇关于自动启动Windows服务几个小时,然后每天自动停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆