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

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

问题描述

我工作的一个程序,将需要删除的文件夹(然后重新初始化它),在一天中的某个时刻,这个时刻将被用户给出。



小时将最有可能是在夜间,因为当没有人访问该文件夹(它的工作时间以外)的。是否有触发在某些时刻该事件的方式?



我知道计时器,但有一个更简单的方法来做到这一点没有蜱,并检查计时器看看它是什么时间



修改:也许我是不够具体。我想触发做一些事情,一种方法,而无需首先编译它在一个单独的可执行文件。这种方法是作为Windows服务来实现一个更大类的一部分。所以这种服务持续运行,但在一天的某个时间,就应该触发这个功能来删除文件夹。



感谢。


< 。DIV CLASS =h2_lin>解决方案

想想开箱



没有必要对这种工作的编码 - 使用<一个HREF =htt​​p://en.wikipedia.org/wiki/Task_Scheduler相对=nofollow>计划任务的,他们一直在窗口很长一段时间。你可以从这个脱掉你的程序



更新:(以下更新质疑)



如果您需要从一个已经的运行业务触发的方法,来对付你的目标时间的计时器和测试 DateTime.Now


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?

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.

Thanks.

解决方案

Think out of the box.

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.

Update: (following update to question)

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天全站免登陆