C# 如何在一天中的特定时间触发事件? [英] C# How can I trigger an event at a specific time of day?

查看:40
本文介绍了C# 如何在一天中的特定时间触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,需要在一天中的某个时间删除一个文件夹(然后重新实例化它),这个时间将由用户指定.

I'm working on a program that will need to delete a folder (and then re-instantiate it) at a certain hour of the day, and this hour will be given by the user.

这个时间很可能是在晚上,因为那时没有人访问文件夹(在工作时间之外).有没有办法在特定时间触发该事件?

The hour will most likely be during the night, because that's when nobody is accessing the folder (it's outside working hours). Is there a way to trigger that event at that certain hour?

我知道计时器,但有没有更简单的方法来做到这一点,而无需计时器来滴答并检查时间?

I know about timers, but is there an easier way to do this without a timer that ticks and checks to see what time it is?

编辑:也许我不够具体.我想触发一个方法来做某事,而不必先在单独的可执行文件中编译它.此方法是作为 Windows 服务实现的更大类的一部分.所以这个服务一直在运行,但是在一天中的某个时间,它应该触发这个函数来删除文件夹.

EDIT: Maybe I wasn't specific enough. I want to trigger a method to do something, without having to first compile it in a separate executable. This method is part of a bigger class that is implemented as a Windows Service. So this service continuously runs, but at a certain time of day, it should trigger this function to delete the folder.

谢谢.

推荐答案

开箱即用.

此类工作无需编码 - 使用 预定任务,它们已经在窗户里呆了很长时间.你可以从这里开始你的程序.

No need for coding on this kind of job - use Scheduled Tasks, they have been in windows for a long time. You can kick off your program from this.

更新:(问题更新后)

如果您需要从已经运行的服务中触发方法,请使用计时器并针对您的目标时间测试 DateTime.Now.

If you need to trigger a method from an already running service, use a timer and test DateTime.Now against your target time.

这篇关于C# 如何在一天中的特定时间触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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